首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在guizero/tkinter中向按钮添加工具提示?

如何在guizero/tkinter中向按钮添加工具提示?
EN

Stack Overflow用户
提问于 2022-08-27 18:36:41
回答 1查看 201关注 0票数 1

我试图添加工具提示到我的Guizero应用程序,但我似乎不能正确地结合它们。当我试图最小化我所使用的库的数量的时候,如果你能给我一些与Guizero一起工作的东西,那就太好了。

代码语言:javascript
复制
from tkinter import *
from tkinter.tix import *
from guizero import *
app=App(width=500,height=500)

#Create a tooltip
tip = Balloon(app)
app.add_tk_widget(Balloon)

#Create a Button widget
my_button=PushButton(app, text= "Hover Me")


#Bind the tooltip with button
tip.tk.bind_widget(my_button,balloonmsg="hovered text")

app.display()

使用平纹机与吉泽罗

我用的是

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-31 12:46:19

你查过idlelib.tooltip了吗?我知道这适用于tkinter,它很可能适用于guizero。

代码语言:javascript
复制
# quick and dirty example
import guizero as gz  # star imports are not your friend
from idlelib.tooltip import Hovertip

# skipping other imports and stuff for brevity...

my_button = gz.PushButton(app, text='Hover Me')  # button (note the 'gz' namespace)

tooltip = Hovertip(
    my_button.tk,  # the widget that gets the tooltip
    # edited to 'mybutton.tk' per acw1668 - thanks!
    'Super helpful tooltip text'
)

就像我说的,我不确定这是否会对吉泽罗有效,但值得一试。

UPDATE:如果要对Hovertip进行样式设置,可以重写Hovertip类以在__init__中包含样式属性

代码语言:javascript
复制
from idlelib.tooltip import OnHoverTooltipBase  # import this instead of 'Hovertip'


# write your own 'Hovertip' class
class Hovertip(OnHoverTooltipBase):
    """Hovertip with custom styling"""
    def __init__(
        self,
        anchor_widget,
        text,
        hover_delay=1000,
        **kwargs,  # here's where you'll add your tk.Label keywords at init
    ):
        super(Hovertip, self).__init__(anchor_widget, hover_delay=hover_delay)
        self.text = text,
        self.kwargs = kwargs
    
    def showcontents(self):
        """Slightly modified to include **kwargs"""
        label = tk.Label(self.tipwindow, text=self.text, **self.kwargs)

现在,当您初始化一个新的Hovertip时,可以向tk.Label添加参数关键字,如下所示

代码语言:javascript
复制
tooltip = Hovertip(
    my_button.tk,
    'Super stylish tooltip text',
    # any keywords that apply to 'tk.Label' will work here!
    background='azure',  # named colors or '#BADA55' hex colors, as usual
    foreground='#123456',
    relief=tk.SOLID,
    borderwidth=2
)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73513504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档