Home > Net >  How to make tkinter's tooltips text change instead of floating on top of each other?
How to make tkinter's tooltips text change instead of floating on top of each other?

Time:12-13

def show_info_for_test(self, chosen_test):
        if chosen_test == "00.NA":
            ToolTip(self.expected_val,msg="No test has been chosen")
        elif chosen_test == "01.Min":
            ToolTip(self.expected_val, msg="Fail if real minimum smaller")
        elif chosen_test == "02.Max":
            ToolTip(self.expected_val, msg="Fail if real max larger")
        elif chosen_test == "03.Min-Max":
            ToolTip(self.expected_val, msg="Fail if not in range")

enter image description here

As you can see the "Fail if not in range" is on top of the "Fail if real minimum smaller" message. The messages are supposed to change according to what I select on the third combobox. I think it is because I probabely create new tooltips with each time I choose something from the combobox. Is there a way to change the text of the tooltip instead of creating new ones?

CodePudding user response:

You shouldn't create new instance of ToolTip whenever show_info_for_test() is called. You should create an instance of ToolTip once and update the tooltip message via the callback passed by the msg option of ToolTip().

Below is an example based on your code:

import tkinter as tk
from tktooltip import ToolTip

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        self.expected_val = tk.Label(self, text="Hello")
        self.expected_val.pack(padx=100, pady=100)

        # initial tooltip message
        self.msg = "Hello" 
        # create the ToolTip() once
        self.tooltip = ToolTip(self.expected_val, msg=self.get_msg)

        # use keys to change the tooltip message
        self.bind("1", lambda e: self.show_info_for_test("00.NA"))
        self.bind("2", lambda e: self.show_info_for_test("01.Min"))
        self.bind("3", lambda e: self.show_info_for_test("02.Max"))
        self.bind("4", lambda e: self.show_info_for_test("03.Min-Max"))

    # function used to return the tooltip message
    def get_msg(self):
        return self.msg

    def show_info_for_test(self, chosen_test):
        if chosen_test == "00.NA":
            self.msg = "No test has been chosen"
        elif chosen_test == "01.Min":
            self.msg = "Fail if real minimum smaller"
        elif chosen_test == "02.Max":
            self.msg = "Fail if real max larger"
        elif chosen_test == "03.Min-Max":
            self.msg = "Fail if not in range"

App().mainloop()

Then you can press "1", "2", "3" or "4" to change the tooltip message.

You can apply the logic to your application.


Edit: Example using combobox to change tooltip message:

import tkinter as tk
from tkinter import ttk
from tktooltip import ToolTip

class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.config(padx=100, pady=100)

        # initial tooltip message
        self.msg = "Hello"

        options = ("00.NA", "01.Min", "02.Max", "03.Min-Max")
        self.combo = ttk.Combobox(self, values=options, state="readonly")
        self.combo.pack(side="left")
        self.combo.bind("<<ComboboxSelected>>", lambda e: self.show_info_for_test(self.combo.get()))

        self.expected_val = ttk.Entry(self, width=20)
        self.expected_val.pack(side="left")

        self.tooltip = ToolTip(self.expected_val, msg=self.get_msg, delay=0.01)

    # function used to return the tooltip message
    def get_msg(self):
        return self.msg

    def show_info_for_test(self, chosen_test):
        if chosen_test == "00.NA":
            self.msg = "No test has been chosen"
        elif chosen_test == "01.Min":
            self.msg = "Fail if real minimum smaller"
        elif chosen_test == "02.Max":
            self.msg = "Fail if real max larger"
        elif chosen_test == "03.Min-Max":
            self.msg = "Fail if not in range"

App().mainloop()

enter image description here

  • Related