【发布时间】:2021-08-23 11:59:34
【问题描述】:
我正在尝试开发一个像 MS Paint 这样的应用程序,但我在制作必须在画布上插入不同形状的代码部分时遇到了麻烦。与此链接中的代码类似:Tkinter resize a rectange on Canvas using mouse,我尝试实现我的代码,但这次我想创建一个三角形,而不是矩形。但它不会在画布上的图像上显示三角形。 (self.b 是一张图片) 我该如何解决这个问题?以及如何将我制作的每个三角形在画布上移动到任何我想要的位置?
#Create triangle:
def on_click3(self, event):
"""fires when user clicks on the background ... creates a new rectangle"""
self.x1 = event.x
self.y1 = event.y
self.x2 = event.x
self.y2 = event.y
self.current =self.create_polygon(self.x1, self.y1, self.x2, self.y1,(self.x1+self.x2)/2,self.y2,self.x1,self.y1, width=self.thickness_entry.get(), outline=self.culoare, tags="Triangle")
self.tag_bind(self.current, '<Button-1>', partial(self.on_click_triangle, self.current))
self.tag_bind(self.current, '<Button1-Motion>', self.on_motion3)
def on_click_triangle(self, tag, event):
"""fires when the user clicks on a rectangle ... edits the clicked on rectange"""
self.current = tag
x1, y1, x2, y2 = self.coords(tag)
if abs(event.x-x1) < abs(event.x-x2):
# opposing side was grabbed; swap the anchor and mobile side
x1, x2 = x2, x1
if abs(event.y-y1) < abs(event.y-y2):
y2 = y1
event.x, event.y = x1, y1
def on_motion3(self, event):
"""fires when the user drags the mouse ... resizes currently active rectangle"""
self.coords(self.current, self.x1 ,self.y1, event.x, event.y)
def instruction_triangle(self):
self.config(cursor="crosshair")
#self.bind("<ButtonPress-1>",self.left_but_down)
#self.bind("<ButtonRelease-1>",self.left_but_up)
self.tag_bind(self.b, '<Button-1>', self.on_click3)
self.tag_bind(self.b, '<Button1-Motion>', self.on_motion3)
【问题讨论】:
-
您要制作规则多边形还是手绘(不规则)多边形?
-
@Derek 通过该命令 create_polygon 是您可以在 tkinter 中创建三角形的唯一方法。结果,我想生成正多边形
-
做了一个小改动(从类中传输代码时发现了一个错误)并添加了一个旋转对象。
an and si变量选择角度和边值。
标签: python tkinter tkinter-canvas