在tkinter python中围绕光标绘制一个定义的大小圆圈

时间:2017-03-06 16:46:22

标签: python canvas tkinter tkinter-canvas

如何在tkinter python中围绕光标绘制一个定义的大小圆圈?

我试过

canvas.config(cursor='circle')

但它绘制了一个特定的圆圈,其大小无法更改。

2 个答案:

答案 0 :(得分:1)

您可以在Tkinter中使用Motion绑定,这会导致每次移动鼠标时都会激活一个函数:

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()

我不建议一般使用全局变量,但对于像这样的简单程序,它没关系。

答案 1 :(得分:0)

您无法绘制自定义光标。你有一组非常有限的游标可供选择。