1

I am trying to add tool tips into my Guizero app but I cant seem to combine them correctly. While I'm trying to minimize the amount of libraries I'm using if you can give me something else that works with Guizero that would be great too.

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()

Using tkinter with guizero

What I'm using

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
PurpleLlama
  • 168
  • 1
  • 2
  • 14

1 Answers1

1

Have you looked into idlelib.tooltip? I know that works with tkinter, it might well work with 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'
)

As I said, I'm not sure this will work with guizero out of the box, but it's worth a shot.

UPDATE: If you want to style the Hovertip, you can override the Hovertip class to include style attributes at __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)

Now when you initialize a new Hovertip, you can add parameter keywords to the tk.Label like so

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
)
JRiggles
  • 4,847
  • 1
  • 12
  • 27
  • 1
    A quick aside, you may want to look at moving away from `tk.tix` and using `ttk` instead - `tk.tix` is no longer maintained and has been replaced by `ttk`. Just change your import to `from tkinter import ttk` and remember to namespace your widgets appropriately, e.g.: `ttk.Frame` – JRiggles Aug 31 '22 at 12:50
  • I don't have any problems importing it but it doesn't work. – PurpleLlama Aug 31 '22 at 18:22
  • 3
    It should be `tooltip = Hovertip(my_button.tk, '...')` instead. – acw1668 Sep 02 '22 at 15:36
  • Do you know where to find the documentation for idlelib.tooltip is I can find it. I want to style some stuff. – PurpleLlama Sep 02 '22 at 22:07
  • 1
    I don’t know an easy way to style them. Looking through the [source code](https://github.com/python/cpython/blob/main/Lib/idlelib/tooltip.py), you can see that the tip itself is just a `Label`, but it’s obfuscated by the `Hovertip.showcontents()` method. – JRiggles Sep 04 '22 at 10:57