简体   繁体   English

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

[英]Python tkinter change how to change cursor without the use of canvas/widgets?

I want to change how to cursor looks while the mouse is in a range of pixels, and when it gets out of that range, it changes to normal (I dont want to use the canvas/other widgets "cursor=" kwarg)我想更改 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()

Unfortunately, the only way to do what you want is to use root.config(cursor="cursor_name") .不幸的是,做你想做的唯一方法是使用root.config(cursor="cursor_name") "watch" cursor_name is for the busy cursor and "" cursor_name is for the normal cursor. "watch" cursor_name 用于繁忙的 cursor, "" cursor_name 用于正常 cursor。

Also you need to change the "<B1-Motion>" event (mouse dragging , with left mouse button pressed) to just "<Motion>" event (mouse movement, it's not necessary to press any mouse buttons).您还需要将"<B1-Motion>"事件(鼠标拖动,按下鼠标左键)更改为"<Motion>"事件(鼠标移动,无需按下任何鼠标按钮)。

And, of course, you need to change the cursor back ( else block)而且,当然,您需要将 cursor 改回来( else阻止)

Here's the fixed code:这是固定代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM