如何为 Tkinter 按钮上的文本描述添加悬停功能? [英] How to add hover feature for text description on a Tkinter button?
问题描述
我想在 Tkinter 按钮上添加悬停功能,如果用户悬停鼠标光标,则会显示描述文本.我还想为该描述添加一些延迟,以免干扰.
I want to add a hover feature on a Tkinter button where if the user hovers the mouse cursor then description text displays. I also want to add some delay for that description to appear so that it would not be intrusive.
我可以尝试使用"<Enter>>>>>
和"<Leave>>>>>>
将按钮绑定到一个函数并制作一些";标签"出现在应用程序的某个角落.但这种方法可能不是最优雅的.
I can try using the "<Enter>"
and "<Leave>"
binding of the button to a function and make some "Label" appear in some corner of the app. But this approach may not be the most elegant.
推荐答案
这是一个使用 Pmw
(python 大型小部件)作为工具提示的小片段.
Here is a small snippet using Pmw
(python mega widgets) for the tool tips.
首先从安装开始:
pip install Pmw
然后这里是一个片段来了解Pmw
可以做什么:
Then here is a snippet to understand what Pmw
can do:
from tkinter import *
import Pmw
root = Tk()
Pmw.initialise(root) #initializing it in the root window
l = Label(root,text='Random Text')
l.pack()
b = Button(root,text='Hover me')
b.pack()
tooltip_1 = Pmw.Balloon(root) #Calling the tooltip
tooltip_1.bind(b,'This is the hover Text
Hope you get an idea of whats going on here.') #binding it and assigning a text to it
root.mainloop()
希望这能给你一个更好的主意.请记住,Pmw
可能会在稍后将 py 转换为 exe 时造成混乱(如果您有任何意图).有一种方法可以解决.
Hope this gives you a better idea. Keep in mind that Pmw
could create a mess while converting the py to an exe later(if you have any intentions to). There is a way around in tho.
干杯
这篇关于如何为 Tkinter 按钮上的文本描述添加悬停功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!