【问题标题】:Draw a defined size circle around cursor in tkinter python在 tkinter python 中围绕光标绘制一个定义大小的圆圈
【发布时间】:2017-03-06 16:46:22
【问题描述】:

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

我试过了

canvas.config(cursor='circle')

但它会画一个特定的圆圈,并且它的大小不能改变。

【问题讨论】:

  • 您是在问如何(在画布上)绘制一个圆,还是在问如何获得一个同时包含指针和圆的光标?
  • 移动光标时需要在光标周围画一个圆圈(与上面的代码相同,但可以更改大小),而不仅仅是画一个圆圈。

标签: python canvas tkinter tkinter-canvas


【解决方案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()

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

【讨论】:

  • 谢谢!这正是我需要的!我会尝试将它添加到我的大程序中。
  • 很高兴能帮上忙!
【解决方案2】:

您不能绘制自定义光标。您可以选择的游标非常有限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-06
    • 2018-08-12
    • 2022-01-20
    • 2012-10-25
    • 2021-06-24
    • 2019-09-04
    • 2021-07-30
    • 2018-09-04
    相关资源
    最近更新 更多