ttkbootstrap icon indicating copy to clipboard operation
ttkbootstrap copied to clipboard

Specify side to display tooltip

Open robert1021 opened this issue 3 years ago • 0 comments

Is your feature request related to a problem? Please describe.

For the the tooltip add in the ability to specify to display it on the left or right.

When using the tooltip if you go fullscreen and your widget is on the right near the edge of the app, some of the tooltip is not visible.

I got around this by adding in the ability to choose which side of the widget to display the tooltip.

Describe the solution you'd like

I added a keyword argument to the constructor and adjusted two of the methods. I played around with the xy coordinates as well. Maybe a way to specify those xy coordinates would be a useful feature?

class ToolTip:

def __init__(
    self,
    widget,
    text="widget info",
    bootstyle=None,
    wraplength=None,
    display_side='right',
    **kwargs,
):

self.widget = widget
    self.text = text
    self.bootstyle = bootstyle
    self.wraplength = wraplength or utility.scale_size(self.widget, 300)
    self.display_side = display_side
    self.toplevel = None

def show_tip(self, *_):
    """Create a show the tooltip window"""
    if self.toplevel:
        return

    if self.display_side == 'right':
        x = self.widget.winfo_pointerx() + 0
        y = self.widget.winfo_pointery() + 19
    elif self.display_side == 'left':
        x = self.widget.winfo_pointerx() - 108
        y = self.widget.winfo_pointery() + 23

    self.toplevel = ttk.Toplevel(position=(x, y), **self.toplevel_kwargs)
    lbl = ttk.Label(
        master=self.toplevel,
        text=self.text,
        justify=LEFT,
        wraplength=self.wraplength,
        padding=10,
    )
    lbl.pack(fill=BOTH, expand=YES)
    if self.bootstyle:
        lbl.configure(bootstyle=self.bootstyle)
    else:
        lbl.configure(style="tooltip.TLabel")

def move_tip(self, *_):
    """Move the tooltip window to the current mouse position within the
    widget.
    """
    if self.toplevel:
        if self.display_side == 'right':
            x = self.widget.winfo_pointerx() + 0
            y = self.widget.winfo_pointery() + 19
        elif self.display_side == 'left':
            x = self.widget.winfo_pointerx() - 108
            y = self.widget.winfo_pointery() + 23
        self.toplevel.geometry(f"+{x}+{y}")
        
        

How to use

ToolTip(widget, text='testing', display_side='left')

Describe alternatives you've considered

No alternatives considered

Additional context

No response

robert1021 avatar Apr 26 '22 16:04 robert1021