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

该博客介绍了如何利用Python的Tkinter库创建一个图形用户界面,实现在该界面上动态缩放任意多边形的功能。用户通过鼠标操作,可以围绕鼠标终点缩放图形,提供了三种不同的缩放方式。代码中详细注释了每个部分的功能,包括图形的绘制、鼠标事件的绑定等,展示了Tkinter在图形交互方面的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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_y
    first_x,first_y = event.x,event.y    
def StopMove(event):
    global first_x,first_x
    cv.delete('L')
    drawPolygon(first_x,first_y,event.x,event.y)
    cv.dtag('L','L')    
def OnMotion(event):
    global first_x,first_x
    cv.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_y
    first_x,first_y = event.x,event.y    
def StopMove(event):
    global first_x,first_x
    cv.delete('L')
    drawPolygon(first_x,first_y,event.x,event.y)
    cv.dtag('L','L')    
def OnMotion(event):
    global first_x,first_x
    cv.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()

代码实现效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值