title | Title of the alert window . |
text | Message or text to be displayed on alert window. |
duration | In Milli seconds, to show the message. If not provided then window closes on click. |
bootstyle | Style keyword, options are primary, secondary, success,info,warning,danger, light, dark |
alert | True / False : To sound the alert beep while displaying the window. |
icon | A unicode character to display on the top-left hand corner of the toast. |
iconfont | The font used to render the icon. |
position | tuple ( x, y, anchor ). 'ne' is the anchor by default. n, e, s, w, nw, ne, sw, se |
**kwargs | Other keyword arguments. |
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from ttkbootstrap.toast import ToastNotification
# Create Main Window
my_w = ttk.Window(themename="lumen")
my_w.geometry("300x200") # Width and height
# Create Toast Notification
toast = ToastNotification(
title="This is title of Notification",
message="Welcome to plus2net.com to learn Python",
position=(400, 300, "ne"),
alert=True,
duration=5000,
bootstyle=SUCCESS
)
# Display the Toast Notification
toast.show_toast()
# Run the Tkinter Main Loop
my_w.mainloop()
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from ttkbootstrap.toast import ToastNotification
# Create Main Window
my_w = ttk.Window(themename="lumen")
my_w.geometry("300x200") # Set window width and height
# Create Toast Notification
toast = ToastNotification(
title="This is the title of Notification", # Notification title
message="Welcome to plus2net.com to learn Python", # Notification message
position=(400, 250, "ne"), # Position at (400, 250) aligned to top-right
bootstyle=DANGER # Use danger style for red theme
)
# Buttons to show and hide the toast notification
b1 = ttk.Button(
my_w,
text="Open Toast",
command=lambda: toast.show_toast(), # Show toast on button click
bootstyle=SUCCESS # Green success button
)
b1.grid(row=1, column=1, padx=10, pady=30) # Position button in grid
b2 = ttk.Button(
my_w,
text="Close Toast",
command=lambda: toast.hide_toast(), # Hide toast on button click
bootstyle=DANGER # Red danger button
)
b2.grid(row=1, column=2, padx=10, pady=30) # Position button in grid
# Run the Tkinter Main Loop
my_w.mainloop()
Here is a list of Unicode symbols that you can use as icons in ToastNotification. Each row includes the symbol, Unicode code, and a working example.
Symbol | Unicode Code | Example Code |
---|---|---|
β Checkmark | "\u2705" |
|
β Cross Mark | "\u274C" |
|
β οΈ Warning | "\u26A0" |
|
βΉοΈ Info | "\u2139" |
|
π Bell | "\U0001F514" |
|
π Trophy | "\U0001F3C6" |
|
π₯ Fire | "\U0001F525" |
|
import ttkbootstrap as ttk
from ttkbootstrap.toast import ToastNotification
root = ttk.Window(themename="superhero")
toast = ToastNotification(
title="Success!",
message="Your operation completed successfully.",
duration=3000, # 3 seconds
icon="\u2705", # β
Checkmark Unicode
bootstyle="success"
)
toast.show_toast()
root.mainloop()
icon
parameter in ToastNotification accepts Unicode symbols to enhance user experience.To enhance user experience, we can customize its position, icon, and interactivity. Let's explore advanced configurations and use cases.
This example demonstrates how to position the toast at specific screen coordinates and use a custom icon.
# Import necessary modules
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from ttkbootstrap.toast import ToastNotification
# Initialize the main window
app = ttk.Window(themename="lumen")
app.geometry("300x200")
# Create a toast notification
toast = ToastNotification(
title="Custom Icon and Position",
message="This toast appears at (500, 500) with a custom icon.",
duration=4000,
bootstyle=SUCCESS,
icon="\u2705", # Checkmark icon
position=(500, 500, 'ne')
)
# Display the toast
toast.show_toast()
# Run the application
app.mainloop()
# Get screen width and height
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# Calculate center position for the toast
toast_x = screen_width // 2
toast_y = screen_height // 2
Check the example given below to position the toast message at center position.
In this example, we create buttons to show and hide the toast dynamically.
# Import necessary modules
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from ttkbootstrap.toast import ToastNotification
# Initialize the main window
app = ttk.Window(themename="lumen")
app.geometry("300x200")
# Create a toast notification
toast = ToastNotification(
title="Interactive Toast",
message="Click buttons to show or hide this toast.",
bootstyle=INFO
)
# Define button commands
def show_toast():
toast.show_toast()
def hide_toast():
toast.hide_toast()
# Create buttons
show_button = ttk.Button(app, text="Show Toast", command=show_toast, bootstyle=SUCCESS)
show_button.pack(pady=10)
hide_button = ttk.Button(app, text="Hide Toast", command=hide_toast, bootstyle=DANGER)
hide_button.pack(pady=10)
# Run the application
app.mainloop()
We will allow users to hover over buttons representing different fruits, and a toast notification will appear with information about the fruit. The notification will disappear once the mouse moves away. Additionally, we will tackle the issue of overlapping toast notifications and how to fix it.
Initially, we encountered an issue where multiple toast notifications were created if the mouse was moved quickly over different buttons. This caused overlapping notifications and a cluttered UI.
The reason for this behavior was that each time the mouse entered a button, a new toast was created without closing the previous one ( Note the time delay in closing ). As a result, multiple toasts remained on the screen at the same time.
To fix this issue, we implemented the following changes:
active_toast
to track the currently active toast.import ttkbootstrap as ttk
from ttkbootstrap.toast import ToastNotification
# Create Main Window
root = ttk.Window(themename="flatly")
root.geometry("500x200")
root.title("Fruit Info with Toast")
# Get screen width and height
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# Calculate center position for the toast
toast_x = screen_width // 2
toast_y = screen_height // 2
# Fruit Data Dictionary
fruits = {
"Apple": "Apples are rich in fiber and vitamin C.",
"Banana": "Bananas provide instant energy and are high in potassium.",
"Cherry": "Cherries are packed with antioxidants and promote sleep.",
"Mango": "Mangoes are high in vitamin A and great for the skin."
}
# Global variable to track active toast
active_toast = None
# Function to Show Toast and Prevent Overlapping
def show_toast(fruit_name):
global active_toast
if active_toast:
active_toast.hide_toast() # Hide the previous toast before creating a new one
# Create a new toast notification
active_toast = ToastNotification(
title="Fruit Info",
message=fruits[fruit_name],
duration=None, # Keep it visible until manually hidden
bootstyle="info",
position=(toast_x, toast_y, "center")
)
active_toast.show_toast()
# Function to Hide Toast Immediately on Mouse Out
def hide_toast(event=None):
global active_toast
if active_toast:
active_toast.hide_toast()
active_toast = None
# Create a Frame for Buttons (Using Grid Layout)
frame = ttk.Frame(root)
frame.grid(row=0, column=0, padx=10, pady=20)
# Add Buttons in a Single Row (Using Grid)
for col, fruit in enumerate(fruits.keys()):
btn = ttk.Button(frame, text=fruit, bootstyle="primary", width=12)
btn.grid(row=0, column=col, padx=10, pady=5)
# Bind mouse events
btn.bind("<Enter>", lambda e, f=fruit: show_toast(f)) # Show toast on hover
btn.bind("<Leave>", hide_toast) # Hide toast immediately on mouse out
# Run Application
root.mainloop()
By properly managing the toast notifications, we ensure a smooth and clean user experience. The use of a global variable to track active toasts and immediate hiding on mouse leave prevents multiple overlapping messages. This method can be further extended for different types of notifications in Tkinter applications.
Now, the toast appears only when needed and disappears immediately when the mouse leaves! π
Toast notifications can be useful in many scenarios, including:
Toast notifications in ttkbootstrap provide an easy way to display alerts and status messages in Tkinter applications. By customizing their position, icons, and interactivity, we can enhance user experience and provide real-time feedback. Whether for form submissions, background processes, or important alerts, toasts are a valuable addition to any GUI.
Showing ToolTip on Mouse HoveringAuthor
π₯ Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and projectβoriented with real examples and source code.