Install pyqt-toast-notification
masterInstall the library via pip to use toast notifications in your PyQt or PySide applications.
pip install pyqt-toast-notificationrepository·master·Indexed 41 days ago
https://github.com/niklashenning/pyqttoastA modern, highly customizable toast notification library for PyQt and PySide applications. It features automatic queuing, fade animations, and flexible positioning relative to the screen or specific widgets. The library provides the Toast class for managing notification lifecycles and ToastPreset for quick styling of Success, Warning, Error, and Information themes in light and dark modes.
Install the library via pip to use toast notifications in your PyQt or PySide applications.
pip install pyqt-toast-notificationTo show a toast, import the Toast class, instantiate it (passing a parent widget), configure its properties, and call .show().
Important: An instance of Toast can only be shown once. To show another notification, you must create a new instance of Toast.
from PyQt6.QtWidgets import QMainWindow, QPushButton
from pyqttoast import Toast, ToastPreset
class Window(QMainWindow):
def __init__(self):
super().__init__(parent=None)
self.button = QPushButton(self)
self.button.setText('Show toast')
self.button.clicked.connect(self.show_toast)
def show_toast(self):
toast = Toast(self)
toast.setDuration(5000) # Hide after 5 seconds
toast.setTitle('Success! Confirmation email sent.')
toast.setText('Check your email to complete signup.')
toast.applyPreset(ToastPreset.SUCCESS) # Apply style preset
toast.show()Configure specific visual elements for a single Toast instance:
toast.setShowIcon(True): Enables the icon. (Default: False)toast.setIcon(ToastIcon.ICON_TYPE): Sets a preset icon. Available: SUCCESS, WARNING, ERROR, INFORMATION, CLOSE. (Default: INFORMATION)toast.setIcon(QPixmap('path/to/icon.png')): Sets a custom image icon.toast.setIconColor(QColor): Sets the icon color. Set to None to show the icon without recoloring it. (Default: #5C5C5C)toast.setIconSize(QSize(w, h)): Sets the icon size. (Default: QSize(18, 18))toast.setShowIconSeparator(bool): Enables/disables the separator between icon and text. (Default: True)toast.setIconSeparatorWidth(int): Width of the icon separator. (Default: 2)toast.setShowCloseButton(bool): Enables/disables the close button. (Default: True)toast.setCloseButtonAlignment(ToastButtonAlignment.ALIGNMENT): Sets alignment. Available: TOP, MIDDLE, BOTTOM. (Default: TOP)toast.setCloseButtonIcon(ToastIcon.ICON_TYPE): Sets the close button icon. (Default: ToastIcon.CLOSE)toast.setCloseButtonIconSize(QSize): Size of the close button icon. (Default: QSize(10, 10))toast.setCloseButtonSize(QSize): Size of the close button. (Default: QSize(24, 24))toast.setDuration(int): Duration in ms. Set to 0 to show forever. (Default: 5000)toast.setShowDurationBar(bool): Enables/disables the duration bar. (Default: True)toast.setFadeInDuration(int) / toast.setFadeOutDuration(int): Animation duration in ms. (Default: 250)toast.setResetDurationOnHover(bool): Whether duration resets when hovering. (Default: True)toast.setStayOnTop(bool): Whether toast stays on top of other windows. (Default: True)toast.setMovePositionWithWidget(bool): If true, toasts move with the widget they are relative to. (Default: True)Instead of setting individual colors, you can use applyPreset() to quickly style a toast using predefined themes.
Available Presets:
ToastPreset.SUCCESSToastPreset.WARNINGToastPreset.ERRORToastPreset.INFORMATIONToastPreset.SUCCESS_DARKToastPreset.WARNING_DARKToastPreset.ERROR_DARKToastPreset.INFORMATION_DARKtoast.applyPreset(ToastPreset.SUCCESS)The following methods are static and configure how all toasts behave globally within the application:
Toast.setPosition(ToastPosition.POSITION): Sets the static position. Available: BOTTOM_LEFT, BOTTOM_MIDDLE, BOTTOM_RIGHT, TOP_LEFT, TOP_MIDDLE, TOP_RIGHT, CENTER. (Default: BOTTOM_RIGHT)Toast.setAlwaysOnMainScreen(True): Forces toasts to always show on the main screen. (Default: False)Toast.setPositionRelativeToWidget(some_widget): Positions toasts relative to a specific widget instead of the screen. (Default: None)Toast.setMaximumOnScreen(int): Limits the number of toasts visible at once. Excess toasts are queued. (Default: 3)Toast.setSpacing(int): Sets vertical spacing between toasts. (Default: 10)Toast.setOffset(x, y): Sets the x and y offset of the toast position. (Default: 20, 45)Toast.setFixedScreen(screen): Sets a fixed screen where toasts will be shown.Use QColor and QFont to fully brand your notifications.
toast.setBackgroundColor(QColor): (Default: #E7F4F9)toast.setTitleColor(QColor): (Default: #000000)toast.setTextColor(QColor): (Default: #5C5C5C)toast.setDurationBarColor(QColor): (Default: #5C5C5C)toast.setIconColor(QColor): (Default: #5C5C5C)toast.setIconSeparatorColor(QColor): (Default: #D9D9D9)toast.setCloseButtonIconColor(QColor): (Default: #000000)toast.setTitleFont(QFont): (Default: Arial, 9, Bold)toast.setTextFont(QFont): (Default: Arial, 9)toast.setBorderRadius(int): Makes corners rounded. (Default: 0)toast.setMinimumWidth(int) / toast.setMaximumWidth(int)toast.setMinimumHeight(int) / toast.setMaximumHeight(int)toast.setFixedSize(QSize): Sets a fixed size (not recommended).toast.setTextSectionSpacing(int): Vertical spacing between title and text. (Default: 8)toast.setMargins(QMargins): Margins around the whole content. (Default: QMargins(20, 18, 10, 18))toast.setIconMargins(QMargins): Margins around the icon.toast.setIconSectionMargins(QMargins): Margins around the icon and separator area.toast.setTextSectionMargins(QMargins): Margins around the title and text area.toast.setCloseButtonMargins(QMargins): Margins around the close button.To display a notification, instantiate a Toast object and call its .show() method. The toast will automatically handle its own timing and disappearance based on the set duration.
toast = Toast()
toast.setTitle("Notification")
toast.setText("This is a message.")
toast.show()Customize the visual style of the toast by setting colors for the background, title, text, icon, and duration bar. You can also set custom fonts for the title and text labels.
toast.setBackgroundColor(QColor("#FFFFFF"))
toast.setTitleColor(QColor("#000000"))
toast.setTextColor(QColor("#333333"))
toast.setTitleFont(QFont("Arial", 12))The Toast class is the primary interface for displaying notification dialogs in PyQt applications. It inherits from QDialog and manages its own lifecycle, including animations (fade in/out), positioning on screen, and a queue system to ensure no more than a specified number of notifications are visible at once.
Key behaviors:
from pyqttoast.toast import Toast
# Basic instantiation
toast = Toast(parent=my_widget)
toast.setTitle("Success")
toast.setText("Operation completed successfully!")
toast.show()You can globally control where toast notifications appear on the screen using Toast.setPosition(position). Use Toast.getPosition() to retrieve the current configuration. The position is defined by the ToastPosition enum.
from pyqttoast import Toast, ToastPosition
# Set toasts to appear at the top left
Toast.setPosition(ToastPosition.TOP_LEFT)
# Check current position
current_pos = Toast.getPosition()Customize the icon used in the toast. You can provide a QPixmap or use a ToastIcon enum. You can also control the icon size, whether the icon is shown, and whether a separator is displayed next to it.
toast.setIcon(ToastIcon.SUCCESS)
toast.setIconSize(QSize(24, 24))
toast.setShowIcon(True)
toast.setShowIconSeparator(True)