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?
I think this would meet your requirements.
Here's what the output looks like:
First, I created a class named
ToolTip
which has methodsshowtip
andhidetip
.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
andy
in the code. The functionCreateToolTip()
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:
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
orToolTip
to avoid confusion.I have a very hacky solution but it has some advantages over the current answers so I figured I would share it.
The only real issue with this is it leaves behind a small box that moves focus away from the main window If anyone knows how to solve these issues let me know
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.
Here's an example that merely updates a label, similar to a statusbar that some apps use.
Here is an example using
<enter>
and<leave>
as @bryanoakley suggested with a toplevel (withoverridedirect
set to true). Use thehover_timer
class for easy use of this. This needs the widget and help-text (with an optional delay argument - default 0.5s) and can be easily called just by initiating the class and then cancelling it.You can refer to this- HoverClass
It is exactly what you need. Nothing more, nothing less
Example app using HoverInfo:
Screenshot: