Dobedani
Dobedani

Reputation: 578

How to make a dynamic tooltip with Tkinter?

I've recently started creating graphical interfaces for my Python code using Tkinter. On my form I usu. have at least 1 text field, i.e. an instance of Tkinter.Entry. Its length is not always sufficient to display the whole text, hence my wish to show a tooltip with the complete text.

The user may update the text field and then the tooltip should then of course show the updated text. That's what I call a dynamic tooltip. I have seen the question What is the simplest way to make tooltips in Tkinter? with several answers - e.g. a nice one from crxguy52 - but none of those tooltips seem to be dynamic. Can anybody suggest what to do, in order to arrange for a dynamic tooltip?

Upvotes: 1

Views: 2087

Answers (2)

Dobedani
Dobedani

Reputation: 578

I have taken the code of crxguy52. In the interface of method __init__ a widget is mentioned. In my case that is the Tkinter.Entry control. In the method showtip I have replaced this line:

label = tk.Label(self.tw, text=self.text, justify='left', 
        background="#ffffff", relief='solid', borderwidth=1,
        wraplength = self.wraplength)

with:

label = tk.Label(self.tw, text=self.widget.get(), justify='left',
        background="#ffffff", relief='solid', borderwidth=1,
        wraplength = self.wraplength)

Now the shown tooltip has become dynamic ;-)

Upvotes: 1

R4PH43L
R4PH43L

Reputation: 2202

Use the answer from the question you linked, store the tooltip reference inside your object and extend the class provided by @crxguy52 by a "text" setter.

Example code:

class CreateToolTip(object):
    """ class from crxguy52 """
    # ...
    # implementation like crxguy showed
    # ...
    def set_text(self, new_text):
        self.text=new_text

Then you can dynamically use tooltip.set_text("this is the new text to show").

Upvotes: 0

Related Questions