如何在 tkinter python 中围绕光标绘制一个定义大小的圆圈?
我试过了
canvas.config(cursor='circle')
但它会绘制一个特定的圆,并且其大小无法更改。
最佳答案
您可以在 Tkinter 中使用 Motion
绑定(bind),这会导致每次移动鼠标时激活一个函数:
import tkinter as tk
global circle
circle = 0
def motion(event):
x, y = event.x + 3, event.y + 7
#the addition is just to center the oval around the center of the mouse
#remove the the +3 and +7 if you want to center it around the point of the mouse
global circle
global canvas
canvas.delete(circle) #to refresh the circle each motion
radius = 20 #change this for the size of your circle
x_max = x + radius
x_min = x - radius
y_max = y + radius
y_min = y - radius
circle = canvas.create_oval(x_max, y_max, x_min, y_min, outline="black")
root = tk.Tk()
root.bind("<Motion>", motion)
global canvas
canvas = tk.Canvas(root)
canvas.pack()
root.mainloop()
一般情况下我不建议使用全局变量,但对于像这样的简单程序来说,这是可以的。
关于python - 在 tkinter python 中围绕光标绘制一个定义大小的圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42631060/