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

标签 python canvas tkinter tkinter-canvas

如何在 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/

相关文章:

javascript - html - 与 css 并排的相对布局

python - Google App Engine模板unicode解码问题

java - `canvas.drawCircle`方法的半径是dp单位吗?

Android:这不是绘制 Canvas 的方式吗?

python - 处理 Tkinter 文本小部件索引系统。

python - 从csv输出python中删除所有逗号分隔

javascript - 用于将任何语言的代码解析为 AST 的 Python 库?

python - 获取 django oscar 中篮子的送货地址

python - 有使用 h5py 在 Python 中对大数据进行分析工作的经验吗?

python - 使用 Tkinter 的 'command = ' 中的 lambda 函数。