Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How do I display tooltips in Tkinter?
Tooltips are helpful UI elements that display additional information when users hover over widgets. In Tkinter, we can create tooltips using the Balloon class from tkinter.tix module.
Using tkinter.tix.Balloon
The Balloon class provides an easy way to add tooltips to any Tkinter widget ?
# Import the tkinter library
from tkinter import *
from tkinter.tix import *
# Create an instance of tkinter frame
win = Tk()
# Set the geometry
win.geometry("600x450")
# Create a tooltip
tip = Balloon(win)
# Create a Button widget
my_button = Button(win, text="Hover Me")
my_button.pack(pady=20)
# Bind the tooltip with button
tip.bind_widget(my_button, balloonmsg="Welcome to TutorialsPoint!")
win.mainloop()
Creating a Custom Tooltip Class
Since tkinter.tix is deprecated, here's a modern approach using a custom tooltip class ?
import tkinter as tk
class ToolTip:
def __init__(self, widget, text):
self.widget = widget
self.text = text
self.tooltip = None
self.widget.bind("<Enter>", self.show_tooltip)
self.widget.bind("<Leave>", self.hide_tooltip)
def show_tooltip(self, event):
x = self.widget.winfo_rootx() + 20
y = self.widget.winfo_rooty() + 20
self.tooltip = tk.Toplevel()
self.tooltip.wm_overrideredirect(True)
self.tooltip.wm_geometry(f"+{x}+{y}")
label = tk.Label(self.tooltip, text=self.text,
background="yellow", relief="solid", borderwidth=1)
label.pack()
def hide_tooltip(self, event):
if self.tooltip:
self.tooltip.destroy()
self.tooltip = None
# Example usage
root = tk.Tk()
root.geometry("400x300")
button1 = tk.Button(root, text="Button 1")
button1.pack(pady=10)
ToolTip(button1, "This is Button 1 tooltip")
button2 = tk.Button(root, text="Button 2")
button2.pack(pady=10)
ToolTip(button2, "This is Button 2 tooltip")
root.mainloop()
Adding Tooltips to Multiple Widgets
You can easily add tooltips to different types of widgets ?
import tkinter as tk
class ToolTip:
def __init__(self, widget, text):
self.widget = widget
self.text = text
self.tooltip = None
self.widget.bind("<Enter>", self.show_tooltip)
self.widget.bind("<Leave>", self.hide_tooltip)
def show_tooltip(self, event):
x = self.widget.winfo_rootx() + 20
y = self.widget.winfo_rooty() + 20
self.tooltip = tk.Toplevel()
self.tooltip.wm_overrideredirect(True)
self.tooltip.wm_geometry(f"+{x}+{y}")
label = tk.Label(self.tooltip, text=self.text,
background="lightyellow", relief="solid", borderwidth=1,
font=("Arial", 9))
label.pack()
def hide_tooltip(self, event):
if self.tooltip:
self.tooltip.destroy()
self.tooltip = None
root = tk.Tk()
root.title("Tooltips Example")
root.geometry("400x250")
# Button with tooltip
btn = tk.Button(root, text="Save File")
btn.pack(pady=10)
ToolTip(btn, "Click to save the current file")
# Entry with tooltip
entry = tk.Entry(root, width=30)
entry.pack(pady=10)
ToolTip(entry, "Enter your name here")
# Label with tooltip
label = tk.Label(root, text="Status: Ready", bg="lightgreen")
label.pack(pady=10)
ToolTip(label, "Current application status")
root.mainloop()
Comparison
| Method | Advantages | Disadvantages |
|---|---|---|
tkinter.tix.Balloon |
Built-in, simple syntax | Deprecated, limited styling |
| Custom ToolTip class | Modern, customizable, works with all widgets | More code required |
Conclusion
While tkinter.tix.Balloon provides a quick solution, creating a custom tooltip class offers better control and modern compatibility. The custom approach works with any Tkinter widget and allows styling customization.
Advertisements
