Tkinter System Tray

Summary: in this tutorial, you will learn how to develop a Tkinter system tray application.

Introduction to Tkinter system tray applications

A system tray application is a type of application that runs in the background and provides quick access to some functions.

The system tray application typically appears as a small icon in the system tray or notification areas in the taskbar of the Windows, macOS, or Linux Desktop environment.

Some common examples of system tray applications are antivirus software, instant messaging clients, or system monitoring software.

Users can interact with the system tray application by clicking on it to access a menu and perform specific actions.

Tkinter lacks built-in support for the system tray application, but you can use the PyStray third-party library to create it.

Tkinter System Tray Application example

Creating a system tray application using the Tkinter and PyStray package involves the following steps:

Step 1. Install the Pystray and Pillow libraries

Install the Pystray and Pillow libraries using the pip command. The Pystray enables you to create a tray application, and the Pillow helps you design an icon placed in the system tray.

pip install pystray pillowCode language: Python (python)

Step 2. Create a Tkinter system tray application

Create a Python script and use the following code:

import tkinter as tk
import pystray
from PIL import Image


class MyApp(tk.Tk):
    def __init__(self):
        super().__init__()

        self.title("System Tray App")
        self.geometry('500x250')
        self.protocol('WM_DELETE_WINDOW', self.minimize_to_tray)
    
    def minimize_to_tray(self):
        self.withdraw()
        image = Image.open("app.ico")
        menu = (pystray.MenuItem('Quit',  self.quit_window), 
                pystray.MenuItem('Show',self.show_window))
        icon = pystray.Icon("name", image, "My App", menu)
        icon.run()

    def quit_window(self, icon):
        icon.stop()
        self.destroy()

    def show_window(self, icon):
        icon.stop()
        self.after(0,self.deiconify)

if __name__ == "__main__":
    app = MyApp()
    app.mainloop()Code language: Python (python)

Output:

Tkinter System Tray Icon

The app.ico can be downloaded here:

How it works.

First, import the necessary libraries:

import tkinter as tk
import pystray
from PIL import ImageCode language: Python (python)

Second, define a new MyApp class that extends the tk.Tk class. In the constructor __init__(), bind the close event with the method minimize_to_tray():

self.protocol('WM_DELETE_WINDOW', self.minimize_to_tray)Code language: Python (python)

When you click the close button, the minimize_to_tray() method will execute creating a system tray icon.

Third, define the minimize_to_tray() method that minimizes the main window to the system tray:

def minimize_to_tray(self):
    self.withdraw()
    image = Image.open("app.ico")
    menu = (pystray.MenuItem('Quit',  self.quit_window), 
            pystray.MenuItem('Show',self.show_window))
    icon = pystray.Icon("name", image, "My App", menu)
    icon.run()Code language: Python (python)

In the method:

The self.withdraw() hides the main window without destroying it. This is the necessary step for minimizing the main window to the system tray:

def minimize_to_tray(self):
    # Hide the main window
    self.withdraw()Code language: Python (python)

The following calls the open() method of the Image class to load the app.ico that will be used as the icon for the system tray:

    # Open the image for the system tray icon
    image = Image.open("app.ico")Code language: Python (python)

The following defines a menu for the system tray icon, which contains two PyStray MenuItem objects:

    menu = (pystray.MenuItem('Quit', self.quit_window), 
            pystray.MenuItem('Show', self.show_window))Code language: Python (python)

The menu items Quit and Show are associated with the self.quit_window and self.show_window methods respectively.

The following line creates a PyStray icon with the specified image and menu:

    icon = pystray.Icon("name", image, "My App", menu)Code language: Python (python)

The following calls the run() method of the Icon object to make the icon active in the system tray:

    icon.run()Code language: Python (python)

The icon.run() line starts the PyStray event loop, making the icon active in the system tray. It will now respond to user interactions, such as clicks on the menu items.

Fourth, define the quit_window() associated with the Quit menu item in the system tray. When you select the Quit option from the system tray icon menu, it stops the PyStray event loop (icon.stop()) and destroys the main window, ensuring a clean exit of the application (self.destroy())

    def quit_window(self, icon):
        icon.stop()
        self.destroy()Code language: Python (python)

Fifth, define the show_window() method associated with the Show menu item in the system tray icon:

    def show_window(self, icon):
        icon.stop()
        self.after(0,self.deiconify)Code language: Python (python)

When you select the Show option from the system tray icon menu it closes the system tray icon (stops the PyStray event loop) and uses the after() method to schedule the deiconify() method to be called immediately, resulting in the main application being restored and becoming visible again.

Summary

  • Use the PyStray library to create a Tkinter system tray application.
Did you find this tutorial helpful ?