Etymology: From "tray" + "-er" (one who creates trays), and coincidentally from Middle English "traitor" β because this library gleefully betrays GNOME 3's philosophy of hiding tray icons. π
Add system tray icons with context menus to your GTK4 applications with just a few lines of code!
- π― Simple API - Add tray icons in 3 lines of code
- π±οΈ Full Click Support - Left, right, and middle-click actions
- π Context Menus - Easy menu creation with separators
- π Dynamic Updates - Change icons and menus at runtime
- π¨ Theme Integration - Uses system icon themes
- π§ Linux Desktop Support - Works on GNOME (with extension), KDE, XFCE, Cinnamon
- π¦ Zero Config - Implements StatusNotifierItem + DBusMenu protocols
pip install trayerSystem Requirements:
# On Debian/Ubuntu:
sudo apt install python3-gi gir1.2-gtk-4.0 python3-dbus
# On GNOME, you also need:
sudo apt install gnome-shell-extension-appindicator
gnome-extensions enable appindicatorsupport@ubuntu.com
# Then logout/loginfrom trayer import TrayIcon
import gi
gi.require_version('Gtk', '4.0')
from gi.repository import Gtk
class MyApp(Gtk.Application):
def do_activate(self):
self.window = Gtk.ApplicationWindow(application=self)
self.window.set_title("My App")
self.window.present()
def toggle_window(self):
if self.window.is_visible():
self.window.hide()
else:
self.window.present()
# Create app
app = MyApp(application_id="com.example.myapp")
# Create tray icon
tray = TrayIcon(
app_id="com.example.myapp",
title="My Application",
icon_name="application-x-executable"
)
# Add click action
tray.set_left_click(app.toggle_window)
# Add menu items
tray.add_menu_item("Show Window", callback=lambda: app.window.present())
tray.add_menu_item("Hide Window", callback=lambda: app.window.hide())
tray.add_menu_separator()
tray.add_menu_item("Quit", callback=app.quit)
# Setup and run (IMPORTANT: setup() before run()!)
tray.setup()
app.run()That's it! π
from trayer import TrayIcon
tray = TrayIcon(
app_id="com.example.myapp", # Your application ID
title="My Application", # Tooltip text
icon_name="application-x-executable" # Icon from theme
)# Left-click
tray.set_left_click(lambda: print("Left clicked!"))
# Middle-click
tray.set_middle_click(lambda: print("Middle clicked!"))
# Right-click automatically shows the menu# Add menu item
tray.add_menu_item("Show", callback=show_window)
# Add disabled item
tray.add_menu_item("Premium Feature", callback=None, enabled=False)
# Add separator
tray.add_menu_separator()
# Add quit button
tray.add_menu_item("Quit", callback=app.quit)# Change icon
tray.change_icon("mail-unread")
# Change status
tray.change_status("NeedsAttention") # Active, Passive, or NeedsAttention
# Update menu dynamically
tray.menu_items.clear()
tray.add_menu_item("New Item", callback=some_function)
tray.update_menu()See examples/ directory for full working examples:
example_minimal.py- Minimal integrationexample_hide_to_tray.py- Hide window to trayexample_dynamic_icon.py- Dynamic icon changesexample_dynamic_menu.py- Dynamic menu updates
Common icon names from system themes:
Applications:
application-x-executableapplications-internetapplications-multimedia
Status:
user-available(green)user-busy(red)user-away(yellow)
Mail:
mail-unreadmail-read
Symbols:
face-smileemblem-importantdialog-information
Find more: Look in /usr/share/icons/ or use gtk4-icon-browser
Create a new tray icon.
Parameters:
app_id(str): Application IDtitle(str): Tooltip texticon_name(str): Icon name from system theme
Set action for left-clicking the tray icon.
Set action for middle-clicking the tray icon.
Add a menu item.
Add a separator line to the menu.
Initialize the tray icon. Must be called before app.run()!
Change the tray icon dynamically.
Change status: "Active", "Passive", or "NeedsAttention".
Update the menu after modifying items dynamically.
Install and enable the AppIndicator extension:
sudo apt install gnome-shell-extension-appindicator
gnome-extensions enable appindicatorsupport@ubuntu.com
# Then logout/loginMake sure you called tray.setup() before app.run()
Ensure callbacks don't take arguments or use lambda:
# β
Correct
tray.add_menu_item("Show", lambda: app.show_window(True))
# β Wrong
tray.add_menu_item("Show", app.show_window(True))This library implements two D-Bus protocols:
-
StatusNotifierItem - The tray icon itself
-
DBusMenu - The context menu
Your application communicates with the desktop environment via D-Bus to display the icon and menu.
- Python 3.8+
- PyGObject (GTK4 bindings)
- dbus-python
- A desktop environment with StatusNotifierItem support (GNOME with extension, KDE, XFCE, Cinnamon)
The name has a double meaning:
- "Tray-er" - One who creates trays (like "player", "baker")
- "Traitor" (Middle English) - Because we gleefully betray GNOME 3's philosophy of removing tray icons!
The GNOME team decided tray icons were "legacy" and removed native support. This library brings them back through the StatusNotifierItem protocol. We're the rebels of the desktop world! π
MIT License - Use freely in your projects!
Contributions are welcome! Please feel free to submit a Pull Request.
- π Bug Reports: GitHub Issues
- π Documentation: GitHub Wiki
- π‘ Feature Requests: GitHub Discussions
Happy betraying! πποΈ
Matteo Benedetto (@Enne2)
- π Location: Italy
- π GitHub: github.com/Enne2
Eclectic systems designer passionate about elegant solutions to complex problems.
MIT License - Use freely in your projects! See LICENSE file for details.
Β© 2025 Matteo Benedetto