Python tkinter change如何在不使用画布/小部件的情况下更改光标?

2024-05-13 15:35:40 发布

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

当鼠标位于像素范围内时,我想更改光标的外观,当鼠标超出该范围时,它会变为正常(我想使用画布/其他小部件“cursor=”kwarg)

from tkinter import*

root = Tk()
root.geometry('500x500+0+0')

def change_cursor(event):
    if event.x in range(450,500):
        #change how the cursor looks


root.bind('<B1-Motion>', change_cursor)
root.mainloop()

Tags: fromimportevent部件tkinter画布root像素
1条回答
网友
1楼 · 发布于 2024-05-13 15:35:40

不幸的是,做你想做的事情的唯一方法就是使用root.config(cursor="cursor_name")"watch"cursor\u name代表忙碌的光标,""cursor\u name代表正常的光标

此外,您还需要将"<B1-Motion>"事件(鼠标拖动,按下鼠标左键)更改为"<Motion>"事件(鼠标移动,无需按下任何鼠标按钮)

当然,您需要将光标更改回(else块)

以下是固定代码:

from tkinter import*


root = Tk()
root.geometry("500x500+0+0")

def change_cursor(event):
    if event.x in range(450, 500):
        root.config(cursor="watch")
    else:
        root.config(cursor="")


root.bind("<Motion>", change_cursor)
root.mainloop()

相关问题 更多 >