到目前为止我已经有了这个代码。起初我让它画圆圈,效果很好。我以为画矩形会很容易,但我只能画正方形。我希望有不同宽度和长度的形状。我知道我需要更改 (x1, y1, x2, y2) 的框,但我该怎么做。
def down(event): # A mouse event will be passed in with x and y attributes
global startx, starty # Use global variables for assignment
startx = event.x # Store the mouse down coordinates in the global variables
starty = event.y
def up(event):
tk_color_string = color(red_intvar, green_intvar, blue_intvar)
r = (startx-event.x)**2 + (starty-event.y)**2 # Pythagorean theorem
r = int(r**.5) # square root to get distance
new_shape = canvas.create_rectangle(startx-r, starty-r, startx+r, starty+r,
fill=tk_color_string, outline='#000000')
shapes.append(new_shape) # aggregate the canvas' item
整个代码超过 100 行,所以我希望这篇文章有助于演示我的要求。 enter image description here
最佳答案
我在下面提供了一个示例代码,我刚刚编写了如何使用鼠标在 tkinter Canvas 中创建矩形的代码。希望这可以帮助您了解所需的相关程序和方法。然后,针对您的特定问题进行调整。希望这对您有帮助并致以最诚挚的问候。
示例代码:如何创建 tkinter Canvas ,允许用户使用鼠标绘制矩形对象。
import tkinter as tk
class App(tk.Frame):
def __init__( self, parent):
tk.Frame.__init__(self, parent)
self._createVariables(parent)
self._createCanvas()
self._createCanvasBinding()
def _createVariables(self, parent):
self.parent = parent
self.rectx0 = 0
self.recty0 = 0
self.rectx1 = 0
self.recty1 = 0
self.rectid = None
def _createCanvas(self):
self.canvas = tk.Canvas(self.parent, width = 800, height = 400,
bg = "white" )
self.canvas.grid(row=0, column=0, sticky='nsew')
def _createCanvasBinding(self):
self.canvas.bind( "<Button-1>", self.startRect )
self.canvas.bind( "<ButtonRelease-1>", self.stopRect )
self.canvas.bind( "<B1-Motion>", self.movingRect )
def startRect(self, event):
#Translate mouse screen x0,y0 coordinates to canvas coordinates
self.rectx0 = self.canvas.canvasx(event.x)
self.recty0 = self.canvas.canvasy(event.y)
#Create rectangle
self.rectid = self.canvas.create_rectangle(
self.rectx0, self.recty0, self.rectx0, self.recty0, fill="#4eccde")
print('Rectangle {0} started at {1} {2} {3} {4} '.
format(self.rectid, self.rectx0, self.recty0, self.rectx0,
self.recty0))
def movingRect(self, event):
#Translate mouse screen x1,y1 coordinates to canvas coordinates
self.rectx1 = self.canvas.canvasx(event.x)
self.recty1 = self.canvas.canvasy(event.y)
#Modify rectangle x1, y1 coordinates
self.canvas.coords(self.rectid, self.rectx0, self.recty0,
self.rectx1, self.recty1)
print('Rectangle x1, y1 = ', self.rectx1, self.recty1)
def stopRect(self, event):
#Translate mouse screen x1,y1 coordinates to canvas coordinates
self.rectx1 = self.canvas.canvasx(event.x)
self.recty1 = self.canvas.canvasy(event.y)
#Modify rectangle x1, y1 coordinates
self.canvas.coords(self.rectid, self.rectx0, self.recty0,
self.rectx1, self.recty1)
print('Rectangle ended')
if __name__ == "__main__":
root = tk.Tk()
root.geometry( "600x400" )
app = App(root)
root.mainloop()
所用步骤和方法概述:
- 在
Tk()
中创建 Canvas 小部件窗口使用tk.Canvas()
方法 - 绑定(bind)鼠标事件
<Button-1>
,<ButtonRelease-1>
和<B1-Motion>
回调到tk.Canvas
小部件。 - 对于每个事件:
3.1 使用方法.canvasx(event.x)
将鼠标指针屏幕x、y坐标转换为 Canvas 坐标和.canvasy(event.y)
并将这些转换后的坐标存储在变量self.rectx0
中,self.recty0
,self.rectx1
,self.recty1
。event.x
和event.y
提供鼠标指针屏幕坐标。
3.2 使用canvas方法.create_rectangle(x0, y0, x1, y1, option, ...)
创建矩形或使用 Canvas 方法.coords(tagOrId, x0, y0, x1, y1)
修改矩形坐标。在这里,x0
,y0
,x1
,y1
表示矩形对象的左上角和右下角 x & y Canvas 坐标,tagOrId
表示矩形对象 ID。
3.3 仅限<Button-1>
,需要将创建的矩形对象的ID存储在变量中。每当.coords() method
时,此变量都会向 tagOrId 提供值。在<B1-Motion>
期间调用和<ButtonRelease-1>
.
关于python - 使用 Tkinter 绘制矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42381856/