Creating a Desktop App with System Tray Integration in Python

schBenedikt - Aug 7 '23 - - Dev Community

If you like my projects, I would be happy about a like and a comment with your experiences!


The given code demonstrates the creation of a desktop application with a graphical user interface (GUI) using the tkinter library in Python. This application features a unique functionality of integrating a system tray icon using the pystray library, enhancing user experience and allowing the application to run in the background while providing quick access to its features.

Upon execution, the code establishes a main application window using the tkinter library. This window is titled "App mit Tray-Icon," signifying its purpose. The GUI design encompasses various elements that contribute to the overall user interaction and experience.

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.title("App mit Tray-Icon")

# GUI elements, widgets, and layout can be added here.
# ...

root.mainloop()
Enter fullscreen mode Exit fullscreen mode

One of the key features of this application is its ability to minimize to the system tray. By clicking the "Minimize" button, the main application window is hidden from view, and an icon representing the application is placed in the system tray area of the operating system's taskbar. This provides users with a convenient way to access the application's functionalities while keeping the main window out of their immediate view.

def minimize_to_tray():
    root.withdraw()  # Hide the main window

restore_button = tk.Button(root, text="Minimize", command=minimize_to_tray)
restore_button.pack()
Enter fullscreen mode Exit fullscreen mode

The "Restore" button, when clicked, brings the main application window back to the foreground from the system tray. This action is implemented through the restore_from_tray() function, ensuring that users can easily switch between interacting with the application and continuing with their other tasks.

def restore_from_tray():
    root.deiconify()  # Show the main window

restore_button = tk.Button(root, text="Restore", command=restore_from_tray)
restore_button.pack()
Enter fullscreen mode Exit fullscreen mode

Additionally, the "Quit" option in the system tray context menu allows users to gracefully exit the application. This action is facilitated by the close_application() function, which stops the system tray icon and then destroys the main application window.

def close_application():
    tray_icon.stop()  # Stop the tray icon
    root.destroy()  # Close the main application

tray_icon.menu = (
    pystray.MenuItem("Restore", restore_from_tray),
    pystray.MenuItem("Minimize", minimize_to_tray),
    pystray.MenuItem("Quit", close_application)
)
Enter fullscreen mode Exit fullscreen mode

The integration of threading is an essential aspect of this code. A separate thread is launched to manage the system tray icon through the tray_icon_thread object. This ensures that the tray icon operates independently from the main application loop, allowing for a smooth and responsive user experience.

import threading

# ...
Enter fullscreen mode Exit fullscreen mode

tray_icon_thread = threading.Thread(target=tray_icon.run)
tray_icon_thread.daemon = True
tray_icon_thread.start()
Furthermore, the application employs the root.protocol("WM_DELETE_WINDOW", minimize_to_tray) statement to ensure that when users click the "close" button on the main application window, the application minimizes to the system tray instead of immediately exiting. This behavior aligns with the application's design philosophy of providing quick access to its features while being unobtrusive.

root.protocol("WM_DELETE_WINDOW", minimize_to_tray)
Enter fullscreen mode Exit fullscreen mode

In summary, the provided code exemplifies the integration of a system tray icon into a graphical desktop application. This feature-rich application allows users to interact with it through both the main application window and the system tray icon, offering a seamless and user-friendly experience.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .