2017-07-19 77 views
-1
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() 

我希望当光标悬停在它上面时,我的光标变成了“手”。当光标离开标签占用的区域时,我也想将光标改回箭头。不过,我得到以下错误:如何将光标悬停在标签上时将其改变为手形?

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Users\Arnob\AppData\Local\Programs\Python\Python36- 
32\lib\idlelib\run.py", line 137, in main 
    seq, request = rpc.request_queue.get(block=True, timeout=0.05) 
    File "C:\Users\Arnob\AppData\Local\Programs\Python\Python36- 
32\lib\queue.py", line 172, in get 
    raise Empty 
queue.Empty 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "C:\Users\Arnob\AppData\Local\Programs\Python\Python36- 
32\lib\tkinter\__init__.py", line 1699, in __call__ 
    return self.func(*args) 
    File "<pyshell#7>", line 1, in <lambda> 
    File "<pyshell#4>", line 2, in changePointer 
    File "C:\Users\Arnob\AppData\Local\Programs\Python\Python36- 
32\lib\tkinter\__init__.py", line 2095, in __getattr__ 
    return getattr(self.tk, attr) 
AttributeError: '_tkinter.tkapp' object has no attribute 'cursor' 

如何改变光标变为手形时,它是由Label占领,然后改回为箭头的区域,当它离开由标签所占用的面积?

+1

我认为,所有你需要的是'链接= LABEL(根,文本=“链接”,光标= “HAND2”)' – Goyo

回答

0

如果你想光标总是手,只是配置标签有一个光标:

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() 
相关问题