Python 3.X 用Thinker做任意多边形的缩放图(一)

时间: 2023-07-09 admin 互联网

Python 3.X 用Thinker做任意多边形的缩放图(一)

Python 3.X 用Thinker做任意多边形的缩放图(一)

Python 3.X 用Thinker做任意多边形的缩放图(需要根据实际图形做修改)

功能介绍一

引用库 thinker

import tkinter as tk

功能介绍二

模块初始化

root = tk.Tk()
root.geometry('600x600')
cv = tk.Canvas(root, height=600, width=600,bg='silver')
cv.pack()
a=[[0.5,0],[1,0.5],[0.5,1],[0,0.5]]

功能介绍三

定义作图函数(具体含义请看注释)

def drawPolygon(x,y,x1,y1):#cv.delete('all') #清除原来图片上痕迹(界面重置)# 注释上句即保留历史痕迹,取消可以重置界面#(1) # 可以围绕鼠标为终点的无数个平行四边形#w,h=x1-x,y1-y#points=[]#for m,n in a:#points.append([x+m*w,y+n*h])#cv.create_polygon(points, outline = "green", fill = "yellow",tags=('L')) #(2)# 可以围绕鼠标为终点的无数个平行四边形(固定大小)'''cv.create_line(x,y,x1,y1, width=1, fill='red') # 任意多边形(直线构成)cv.create_line(x-20,y-30,x,y, width=1, fill='red') # cv.create_line(x+25,y+20,x-20,y-30, width=1, fill='red') # 不与deta相乘即为不放大图片cv.create_line(x+35,y+36,x+25,y+20, width=1, fill='red') # (不放大图形参考注释部分代码)cv.create_line(x+5,y+8,x+35,y+36, width=1, fill='red') #cv.create_line(x1,y1,x+5,y+8, width=1, fill='red') # 完成封闭多边形的绘制 '''   #(3)# 可以围绕鼠标为终点的无数个平行四边形(根据鼠标键值按比例放大与缩小任意类型多边形(曲线或直线构成))deta = abs(x1-x)/5 ##缩放比例的调整deta2 = abs(y1-y)/5 ##缩放比例的调整cv.create_line(x,y,x1,y1, width=1, fill='red') # 任意多边形(直线构成)cv.create_line(x-20*deta,y-30*deta2,x,y, width=1, fill='red') # cv.create_line(x+25*deta,y+20*deta2,x-20*deta,y-30*deta2, width=1, fill='red') # 不与deta相乘即为不放大图片cv.create_line(x+35*deta,y+36*deta2,x+25*deta,y+20*deta2, width=1, fill='red') #cv.create_line(x+5*deta,y+8*deta2,x+35*deta,y+36*deta2, width=1, fill='red') #(不放大图形参考上部分注释部分代码)cv.create_line(x1,y1,x+5*deta,y+8*deta2, width=1, fill='red') # 完成封闭多边形的绘制(放大与缩小)def StartMove(event):    global first_x,first_yfirst_x,first_y = event.x,event.y    
def StopMove(event):global first_x,first_xcv.delete('L')drawPolygon(first_x,first_y,event.x,event.y)cv.dtag('L','L')    
def OnMotion(event):global first_x,first_xcv.delete('L')drawPolygon(first_x,first_y,event.x,event.y)

功能介绍四

鼠标与作图事件绑定

cv.bind("<ButtonPress-1>",StartMove)  #绑定鼠标左键按下事件
cv.bind("<ButtonRelease-1>",StopMove) #绑定鼠标左键松开事件
#cv.bind("<B1-Motion>", OnMotion)      #绑定鼠标左键被按下时移动鼠标事件
#取消最后一个事件绑定会有很多的历史轨迹,反之则保留最后一次数据

功能介绍五

主函数事件

root.mainloop()

完整的代码

import tkinter as tk
root = tk.Tk()
root.geometry('600x600')
cv = tk.Canvas(root, height=600, width=600,bg='silver')
cv.pack()
a=[[0.5,0],[1,0.5],[0.5,1],[0,0.5]]def drawPolygon(x,y,x1,y1):#cv.delete('all') #清除原来图片上痕迹(界面重置)# 注释上句即保留历史痕迹,取消可以重置界面#(1) # 可以围绕鼠标为终点的无数个平行四边形#w,h=x1-x,y1-y#points=[]#for m,n in a:#points.append([x+m*w,y+n*h])#cv.create_polygon(points, outline = "green", fill = "yellow",tags=('L')) #(2)# 可以围绕鼠标为终点的无数个平行四边形(固定大小)'''cv.create_line(x,y,x1,y1, width=1, fill='red') # 任意多边形(直线构成)cv.create_line(x-20,y-30,x,y, width=1, fill='red') # cv.create_line(x+25,y+20,x-20,y-30, width=1, fill='red') # 不与deta相乘即为不放大图片cv.create_line(x+35,y+36,x+25,y+20, width=1, fill='red') # (不放大图形参考注释部分代码)cv.create_line(x+5,y+8,x+35,y+36, width=1, fill='red') #cv.create_line(x1,y1,x+5,y+8, width=1, fill='red') # 完成封闭多边形的绘制 '''   #(3)# 可以围绕鼠标为终点的无数个平行四边形(根据鼠标键值按比例放大与缩小任意类型多边形(曲线或直线构成))deta = abs(x1-x)/5 ##缩放比例的调整deta2 = abs(y1-y)/5 ##缩放比例的调整cv.create_line(x,y,x1,y1, width=1, fill='red') # 任意多边形(直线构成)cv.create_line(x-20*deta,y-30*deta2,x,y, width=1, fill='red') # cv.create_line(x+25*deta,y+20*deta2,x-20*deta,y-30*deta2, width=1, fill='red') # 不与deta相乘即为不放大图片cv.create_line(x+35*deta,y+36*deta2,x+25*deta,y+20*deta2, width=1, fill='red') #cv.create_line(x+5*deta,y+8*deta2,x+35*deta,y+36*deta2, width=1, fill='red') #(不放大图形参考上部分注释部分代码)cv.create_line(x1,y1,x+5*deta,y+8*deta2, width=1, fill='red') # 完成封闭多边形的绘制(放大与缩小)def StartMove(event):    global first_x,first_yfirst_x,first_y = event.x,event.y    
def StopMove(event):global first_x,first_xcv.delete('L')drawPolygon(first_x,first_y,event.x,event.y)cv.dtag('L','L')    
def OnMotion(event):global first_x,first_xcv.delete('L')drawPolygon(first_x,first_y,event.x,event.y)cv.bind("<ButtonPress-1>",StartMove)  #绑定鼠标左键按下事件
cv.bind("<ButtonRelease-1>",StopMove) #绑定鼠标左键松开事件
#cv.bind("<B1-Motion>", OnMotion)      #绑定鼠标左键被按下时移动鼠标事件
root.mainloop()

代码实现效果