In this Tkinter tutorial we will be exploring how to create “Tooltips” in Tkinter. Tooltips are a very common way for us (as programmers) to improve and make our GUI application more User friendly.
Tooltips are used to provide additional information or context about the element being hovered over, and they can be helpful for users who are trying to learn more about a particular feature or option in the GUI.
Tkinter Tooltips Tutorial
We will explain how to create Tooltips in a step-by-step process, showing you one code snippet at a time.
First we will create our basic window, along with a single widget. We will be binding two events to this widget, an “Enter” event (when the mouse cursor is over a widget), and the “Leave” event (when the mouse moves away from the widget).
We will also initialize our tooltip window object to “None”. Declaring it with self is important so that all functions we define in this class can access it.
import tkinter as tk
root = tk.Tk()
class Window:
def __init__(self, master):
self.tooltip_window = None
widget = tk.Button(master, text= "Submit")
widget.bind("<Enter>", self.show_tooltip)
widget.bind("<Leave>", self.hide_tooltip)
widget.pack(padx = 50, pady = 50)
The show_tooltip
function is called whenever the mouse hovers over the button widget, and it creates a new top-level window containing the tooltip text. The hide_tooltip
function is called whenever the mouse leaves the button widget, and destroys the tooltip.
We haven’t actually defined these two functions though, so let’s begin doing that. First, we will define the show_tooltip()
function.
What is happening here, is that we create a new TopLevel window with a single label widget (which contains the tooltip text). We need to get rid of the “window decorations” (title, borders, and buttons for minimizing, maximizing, and closing), which we can achieve using the overrideredirect(True)
method.
# Define a function that will be called when the mouse hovers over the widget
def show_tooltip(self, event):
# Create a new top-level window with the tooltip text
self.tooltip_window = tk.Toplevel(root)
tooltip_label = tk.Label(self.tooltip_window,
text="This button will submit your data")
tooltip_label.pack()
# Use the overrideredirect method to remove the window's decorations
self.tooltip_window.overrideredirect(True)
# Calculate the coordinates for the tooltip window
x = root.winfo_pointerx() + 20
y = root.winfo_pointery() + 20
self.tooltip_window.geometry("+{}+{}".format(x, y))
The last few lines of the above code are responsible for correctly positioning the widget using the geometry method.
Now we define the hide_tooltip method, which is going to be alot simpler. All we need to do is call the destroy()
method on our tooltip window, and then reset the object back to None.
# Define a function that will be called when the mouse leaves the widget
def hide_tooltip(self, event):
# Destroy the tooltip window
self.tooltip_window.destroy()
self.tooltip_window = None
Here is the full code along with the output.
import tkinter as tk
root = tk.Tk()
class Window:
def __init__(self, master):
self.tooltip_window = None
widget = tk.Button(master, text= "Submit")
widget.bind("<Enter>", self.show_tooltip)
widget.bind("<Leave>", self.hide_tooltip)
widget.pack(padx = 50, pady = 50)
# Define a function that will be called when the mouse hovers over the widget
def show_tooltip(self, event):
# Create a new top-level window with the tooltip text
self.tooltip_window = tk.Toplevel(root)
tooltip_label = tk.Label(self.tooltip_window,
text="This button will submit your data")
tooltip_label.pack()
# Use the overrideredirect method to remove the window's decorations
self.tooltip_window.overrideredirect(True)
# Calculate the coordinates for the tooltip window
x = root.winfo_pointerx() + 20
y = root.winfo_pointery() + 20
self.tooltip_window.geometry("+{}+{}".format(x, y))
# Define a function that will be called when the mouse leaves the widget
def hide_tooltip(self, event):
# Destroy the tooltip window
self.tooltip_window.destroy()
self.tooltip_window = None
window = Window(root)
root.mainloop()

If you are looking for an easier way to create tooltips, then take a look at the below video from our YouTube channel. Here we explore how to create Tooltips using a special library which can easily create tooltips with various customizations.
This marks the end of the Tkinter Tooltips Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions about the tutorial content can be asked in the comments section below.