tkinter tooltip quite always not showing in Windows 10 Enterprise

45 views Asked by At

Trying the first basilar example:

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

app = tk.Tk()
b = ttk.Button(app, text="Button")
b.pack()
ToolTip(b, msg="Hover info")
app.mainloop()

with Python 3.12.2, tkinter-tooltip 3.0.0 on Windows 10 Enterprise, tooltips are not displayed in most cases. Sometimes, they appear in random attempts.

2

There are 2 answers

4
Suramuthu R On

Edit: I found that your coding is working well linux. Also With your experiments, We may have to conclude that the problem occurs in windows

Tooltip module's official document saysCertain options do not match great with each other, a good example is follow and delay using small x/y offsets. This can cause the tooltip to appear inside the widget. Hovering over the tooltip will cause it to disappear and reappear, in a new position, potentially again inside the widget. The proof of Screenshot is given here.

So, I just thought of bringing the same result in tkinter without using the tooltip module. And it is successful. Kindly try in your windows PC.

import tkinter as tk
import tkinter.ttk as ttk


def popup(wdgt, txt):
    pop = tk.Toplevel(wdgt)
    pop.overrideredirect(True)
    
    tk.Label(pop, text = txt).pack()
    wdgt.bind('<Leave>', lambda x: pop.destroy())
    x_center =b.winfo_rootx() + b.winfo_width() 
    y_center = b.winfo_rooty() + b.winfo_height()
    pop.geometry(f"+{x_center}+{y_center}")


def make_tt(wdgt, txt):
    
    wdgt.bind('<Enter>', lambda x: popup(wdgt,txt))

app = tk.Tk()
app.geometry('200x300')
b = ttk.Button(app, text="Button")
b.pack()

app.after_idle(lambda : make_tt(b, 'Hover Info'))
app.mainloop()
0
Luca On

Great! you solved my problem!

Following, the code revised as to make possible to have multiple tooltips for more than one button.

import tkinter as tk
import tkinter.ttk as ttk


def popup(wdgt, txt):
    pop = tk.Toplevel(wdgt)
    pop.overrideredirect(True)

    tk.Label(pop, text = txt).pack()
    wdgt.bind('<Leave>', lambda x: pop.destroy())
    x_center =wdgt.winfo_rootx() + wdgt.winfo_width() 
    y_center = wdgt.winfo_rooty() + wdgt.winfo_height()
    pop.geometry(f"+{x_center}+{y_center}")


def make_tt(wdgt, txt):

    wdgt.bind('<Enter>', lambda x: popup(wdgt,txt))

app = tk.Tk()
app.geometry('200x300')
b = ttk.Button(app, text="Button")
b.pack()

app.after_idle(lambda : make_tt(b, 'Hover Info'))
app.mainloop()

Thank you very much again!! Luca