How to make a system tray application in Tkinter?

A System Tray application runs continuously in the background, remaining accessible through the system taskbar even when the main window is closed. This is useful for applications that need to run persistently while minimizing screen space usage.

To create a System Tray application in Tkinter, we use the pystray module, which provides functions to create and manage system tray icons with contextual menus.

Installation

First, install the required package using pip ?

pip install pystray

Creating a System Tray Application

Here's how to create a Tkinter application that minimizes to the system tray ?

# Import required libraries
from tkinter import *
from pystray import MenuItem as item
import pystray
from PIL import Image
import io
import base64

# Create an instance of tkinter window
win = Tk()
win.title("System Tray Application")
win.geometry("400x200")

# Create a simple icon programmatically (since we can't load external files)
def create_icon():
    # Create a simple 64x64 icon
    img = Image.new('RGB', (64, 64), color='blue')
    return img

# Define function to quit the application completely
def quit_window(icon, item):
    icon.stop()
    win.destroy()

# Define function to show the window again
def show_window(icon, item):
    icon.stop()
    win.after(0, win.deiconify)

# Hide window and show in system tray
def hide_window():
    win.withdraw()
    image = create_icon()
    menu = (item('Show', show_window), item('Quit', quit_window))
    icon = pystray.Icon("MyApp", image, "My System Tray Icon", menu)
    icon.run()

# Add some content to the window
label = Label(win, text="This is a System Tray Application", font=("Arial", 16))
label.pack(pady=50)

button = Button(win, text="Minimize to Tray", command=hide_window)
button.pack(pady=10)

# Handle window close button to minimize to tray instead of closing
win.protocol('WM_DELETE_WINDOW', hide_window)

win.mainloop()

How It Works

The application follows these key steps:

  1. Window Creation: A standard Tkinter window is created with basic content.
  2. Hide Function: hide_window() withdraws the window and creates a system tray icon.
  3. Tray Menu: The tray icon includes a context menu with "Show" and "Quit" options.
  4. Protocol Override: The window close button is overridden to minimize to tray instead of closing.

Key Components

System Tray Icon

# Create tray icon with menu
icon = pystray.Icon("MyApp", image, "Tooltip Text", menu)
icon.run()  # Starts the tray icon

Context Menu Items

# Define menu items with callbacks
menu = (
    item('Show', show_window),
    item('Quit', quit_window)
)

Important Notes

  • The win.withdraw() method hides the window completely
  • icon.stop() removes the tray icon before showing the window again
  • win.after(0, win.deiconify) safely shows the window from the tray thread
  • Always provide both "Show" and "Quit" options in the tray menu for good user experience

Conclusion

Using pystray with Tkinter allows you to create professional system tray applications. The key is properly handling the window hide/show cycle and providing an intuitive tray menu for user interaction.

Updated on: 2026-03-25T23:34:35+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements