用Tkinter画一个矩形?

2024-06-16 09:22:04 发布

您现在位置:Python中文网/ 问答频道 /正文

到目前为止我有这个密码。起初我让它画圆圈,效果很好。我以为画长方形也很容易,但我只能画正方形。我希望有各种宽度和长度的形状。我知道我需要换(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行,所以我希望这篇文章能帮助说明我的要求


Tags: theineventnewstringdefvariablesglobal