如何仅当光标悬停在标签上时才将光标更改为指针?

2024-09-30 10:31:58 发布

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

from tkinter import *
root = Tk()
def changeCursor(event, pointerName):
    root.cursor(pointerName)
link = Label(root, text="Link")
link.bind("<Motion>", lambda event : changeCursor(event, "hand"))
link.pack()
root.mainloop()

当光标悬停在指针上时,我希望光标变成“手”。我还想在光标离开标签所占据的区域时将光标改回箭头。但是我得到了以下错误:

^{pr2}$

当光标位于标签所占据的区域时,如何将光标更改为手,然后在光标离开标签所占据的区域时将其改回箭头?在


Tags: fromimportevent区域tkinterdeflinkroot
2条回答
import tkinter as tk    
root = tk.Tk()    
myLabel= tk.Label(root, text="Click Me", cursor="hand2")
myLabel.pack()    
root.mainloop()

如果希望光标始终是指针,只需将标签配置为具有该光标:

import tkinter as tk

root = tk.Tk()

label = tk.Label(root, text="Hello, world", background="bisque", cursor="hand1")
label.pack(side="top", fill="x", padx=10, pady=10)

root.mainloop()

相关问题 更多 >

    热门问题