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
Display message when hovering over something with mouse cursor in Tkinter Python
Let us suppose we want to create an application where we want to add some description on Tkinter widgets such that it displays tooltip text while hovering on the button widget. It can be achieved by adding a tooltip or popup.
Tooltips are useful in applications where User Interaction is required. We can define the tooltip by instantiating the constructor of Balloon(win) from tkinter.tix. After that, we can bind the button with the tooltip message that applies on the widget.
Using tkinter.tix for Tooltips
The tkinter.tix module provides the Balloon widget for creating tooltips. Here's how to implement it ?
# Import the tkinter library
from tkinter import *
from tkinter.tix import *
# Create an instance of tkinter frame
win = Tk()
# Set the geometry
win.geometry("400x200")
# Create a tooltip
tip = Balloon(win)
# Create a Button widget
my_button = Button(win, text="Python", font=('Helvetica bold', 20))
my_button.pack(pady=20)
# Bind the tooltip with button
tip.bind_widget(my_button, balloonmsg="Python is an interpreted, high-level and general-purpose programming language")
win.mainloop()
Custom Tooltip Implementation
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=None):
x, y, _, _ = self.widget.bbox("insert")
x += self.widget.winfo_rootx() + 20
y += self.widget.winfo_rooty() + 20
self.tooltip = tk.Toplevel(self.widget)
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=None):
if self.tooltip:
self.tooltip.destroy()
self.tooltip = None
# Create main window
root = tk.Tk()
root.geometry("300x200")
root.title("Tooltip Example")
# Create button with tooltip
button = tk.Button(root, text="Hover Me!", font=('Arial', 14))
button.pack(pady=50)
# Add tooltip to button
ToolTip(button, "This is a custom tooltip message!")
root.mainloop()
Output
Running the above code will display a window with a button. When you hover over the button, a tooltip appears with the specified message.
Key Points
- The
tkinter.tixmodule is deprecated in newer Python versions - Custom tooltip implementation gives more control over appearance
- Use
<Enter>and<Leave>events to show/hide tooltips - Tooltips enhance user experience by providing helpful information
Conclusion
Tooltips are essential for creating user-friendly GUI applications. While tkinter.tix provides basic tooltip functionality, custom implementations offer better control and modern compatibility for displaying helpful messages on hover.
