I have a GUI made with TKinter in Python. I would like to be able to display a message when my mouse cursor goes, for example, on top of a label or button. The purpose of this is to explain to the user what the button/label does or represents.
Is there a way to display text when hovering over a tkinter object in Python?
A Hover text building block searches for a piece of text on the whole screen or part of the screen and then moves the mouse pointer to hover in the location where the text was found. Typically, this block is used for hovering on a button or a menu item.
Many graphical web browsers display the title attribute of an HTML element as a tooltip when a user hovers the pointer over that element; in such a browser, when hovering over Wikipedia images and hyperlinks a tooltip will appear.
I think this would meet your requirements.
Here's what the output looks like:
First, A class named ToolTip
which has methods showtip
and hidetip
is defined as follows:
from tkinter import * class ToolTip(object): def __init__(self, widget): self.widget = widget self.tipwindow = None self.id = None self.x = self.y = 0 def showtip(self, text): "Display text in tooltip window" self.text = text if self.tipwindow or not self.text: return x, y, cx, cy = self.widget.bbox("insert") x = x + self.widget.winfo_rootx() + 57 y = y + cy + self.widget.winfo_rooty() +27 self.tipwindow = tw = Toplevel(self.widget) tw.wm_overrideredirect(1) tw.wm_geometry("+%d+%d" % (x, y)) label = Label(tw, text=self.text, justify=LEFT, background="#ffffe0", relief=SOLID, borderwidth=1, font=("tahoma", "8", "normal")) label.pack(ipadx=1) def hidetip(self): tw = self.tipwindow self.tipwindow = None if tw: tw.destroy() def CreateToolTip(widget, text): toolTip = ToolTip(widget) def enter(event): toolTip.showtip(text) def leave(event): toolTip.hidetip() widget.bind('<Enter>', enter) widget.bind('<Leave>', leave)
The widget is where you want to add the tip. For example, if you want the tip when you hover over a button or entry or label, the instance of the same should be provided at the call time.
Quick note: the code above uses from tkinter import *
which is not suggested by some of the programmers out there, and they have valid points. You might want to make necessary changes in such case.
To move the tip to your desired location, you can change x
and y
in the code. The function CreateToolTip()
helps to create this tip easily. Just pass the widget and string you want to display in the tipbox to this function, and you're good to go.
This is how you call the above part:
button = Button(root, text = 'click mem') button.pack() CreateToolTip(button, text = 'Hello World\n' 'This is how tip looks like.' 'Best part is, it\'s not a menu.\n' 'Purely tipbox.')
Do not forget to import the module if you save the previous outline in different python file, and don't save the file as CreateToolTip
or ToolTip
to avoid confusion. This post from Fuzzyman shares some similar thoughts, and worth checking out.
You need to set a binding on the <Enter>
and <Leave>
events.
Note: if you choose to pop up a window (ie: a tooltip) make sure you don't pop it up directly under the mouse. What will happen is that it will cause a leave event to fire because the cursor leaves the label and enters the popup. Then, your leave handler will dismiss the window, your cursor will enter the label, which causes an enter event, which pops up the window, which causes a leave event, which dismisses the window, which causes an enter event, ... ad infinitum.
For simplicity, here's an example that updates a label, similar to a statusbar that some apps use. Creating a tooltip or some other way of displaying the information still starts with the same core technique of binding to <Enter>
and <Leave>
.
import Tkinter as tk class Example(tk.Frame): def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) self.l1 = tk.Label(self, text="Hover over me") self.l2 = tk.Label(self, text="", width=40) self.l1.pack(side="top") self.l2.pack(side="top", fill="x") self.l1.bind("<Enter>", self.on_enter) self.l1.bind("<Leave>", self.on_leave) def on_enter(self, event): self.l2.configure(text="Hello world") def on_leave(self, enter): self.l2.configure(text="") if __name__ == "__main__": root = tk.Tk() Example(root).pack(side="top", fill="both", expand="true") root.mainloop()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With