我试图添加工具提示到我的Guizero应用程序,但我似乎不能正确地结合它们。当我试图最小化我所使用的库的数量的时候,如果你能给我一些与Guizero一起工作的东西,那就太好了。
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()我用的是
发布于 2022-08-31 12:46:19
你查过idlelib.tooltip了吗?我知道这适用于tkinter,它很可能适用于guizero。
# 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__中包含样式属性
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添加参数关键字,如下所示
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
)https://stackoverflow.com/questions/73513504
复制相似问题