将光标置于按钮上方时如何在窗口中显示文本。
我有下面的代码,当将光标放在按钮“确定”上时,它必须显示文本为“在按确定之前检查详细信息”。
import Tkinter
class Example(Tkinter.Frame):
def __init__(self, *args, **kwargs):
Tkinter.Frame.__init__(self, *args, **kwargs)
self.l1 = Tkinter.Label(self, text="Enter name")
self.l2 = Tkinter.Label(self, text="", width=40)
self.l1.pack(side="top")
self.l2.pack(side="top", fill="x")
self.b1 = Tkinter.Button(root, text="Ok")
self.b1.bind("<Enter>", self.on_enter)
self.b1.bind("<Leave>", self.on_leave)
self.b1.pack()
def on_enter(self, event):
self.l2.configure(text="Check details filled before pressing Ok")
def on_leave(self, enter):
self.l2.configure(text="")
if __name__ == "__main__":
root = Tkinter.Tk()
Example(root).pack(side="top", fill="both", expand="true")
root.mainloop()
如果我将光标放在标签l1上以显示文本,则相同的代码可以正常工作。将光标放在按钮上或进行任何修改时,还有其他方法可以继续显示吗?
如果我对问题的理解正确,那么您希望当鼠标悬停在不同的小部件上时能够显示不同的消息。最简单的解决方案是向包含消息的每个窗口小部件对象添加一个自定义属性,然后从绑定函数的属性中获取该消息。
例如:
class Example(...):
def __init__(...):
...
self.l1.description = "This is label 1"
self.l2.description = "This is label 2"
self.b1.description = "This is the OK button"
for widget in (self.l1, self.l2, self.b1):
widget.bind("<Enter>", self.on_enter)
widget.bind("<Leave>", self.on_leave)
...
def on_enter(self, event):
description = getattr(event.widget, "description", "")
self.l2.configure(text=description)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句