我试图让标签只有在鼠标悬停时才加下划线。我大部分都遇到了麻烦。我认为有一种更简单的方法可以做到这一点,但我从来没有玩过 tkFont。当鼠标离开标签时,标签也会调整大小。
from Tkinter import *
import tkFont
def move1(event):
f = tkFont.Font(lbl1, lbl1.cget("font"))
f.configure(underline = True)
lbl1.configure(font=f)
def _move1(event):
f.configure(underline = False)
lbl1.configure(font=f)
def move2(event):
f = tkFont.Font(lbl2, lbl2.cget("font"))
f.configure(underline = True)
lbl2.configure(font=f)
def _move2(event):
f.configure(underline = False)
lbl2.configure(font=f)
def move3(event):
f = tkFont.Font(lbl3, lbl3.cget("font"))
f.configure(underline = True)
lbl3.configure(font=f)
def _move3(event):
f.configure(underline = False)
lbl3.configure(font=f)
root=Tk()
root.geometry('100x100+100+100')
f = tkFont.Font()
lbl1 = Label(root, text='Label 1')
lbl1.bind('<Enter>', move1)
lbl1.bind('<Leave>', _move1)
lbl1.pack()
lbl2 = Label(root, text='Label 2')
lbl2.bind('<Enter>', move2)
lbl2.bind('<Leave>', _move2)
lbl2.pack()
lbl3 = Label(root, text='Label 3')
lbl3.bind('<Enter>', move3)
lbl3.bind('<Leave>', _move3)
lbl3.pack()
mainloop()