[파이썬] PyQt 트레이 아이콘 (`QSystemTrayIcon`)

In this blog post, we will explore how to use PyQt to create a tray icon for your desktop application. The tray icon, also known as system tray or notification area icon, provides a convenient way for users to access your application’s features without having to keep the main window open.

What is PyQt?

PyQt is a set of Python bindings for the Qt application framework. It allows developers to create cross-platform applications with native capabilities and performance. PyQt includes a wide range of modules and classes that enable interaction with the system tray, such as QSystemTrayIcon.

Getting Started

To get started, make sure you have PyQt installed. You can install it using pip by running the following command:

pip install pyqt5

Creating the Tray Icon

Let’s start by importing the necessary modules and creating a basic PyQt application window.

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow

app = QApplication(sys.argv)
window = QMainWindow()
window.setWindowTitle("Tray Icon Demo")

Now, we can proceed to create the tray icon and add it to our application.

from PyQt5.QtWidgets import QSystemTrayIcon, QMenu

tray_icon = QSystemTrayIcon(window)
tray_icon.setIcon(window.windowIcon())
tray_icon.setVisible(True)

In the code above, we create a QSystemTrayIcon object and set its icon to the same icon as the application window. We then make the tray icon visible.

To add functionality to our tray icon, we can create a context menu with actions.

menu = QMenu(window)
action_open = menu.addAction("Open")
action_quit = menu.addAction("Quit")

tray_icon.setContextMenu(menu)

In this example, we create two actions: “Open” and “Quit”. You can customize these actions to suit the needs of your application. We then set the context menu of the tray icon to the QMenu we just created.

Handling Tray Icon Actions

To handle the actions triggered from the tray icon’s context menu, we need to connect them to specific functions.

def open_window():
    window.show()

def quit_application():
    app.quit()

action_open.triggered.connect(open_window)
action_quit.triggered.connect(quit_application)

In the code above, we define two functions open_window and quit_application to handle the corresponding actions. We then connect these functions to the triggered signals of the actions.

Running the Application

Finally, we can run our application and see the tray icon in action.

sys.exit(app.exec_())

When you run the code, you should see the tray icon appearing in your system tray/notification area. Right-clicking on the icon will display the context menu with the “Open” and “Quit” actions. Clicking on “Open” will show the application window, and clicking on “Quit” will exit the application.

Conclusion

Adding a tray icon to your PyQt application can improve the user experience and provide quick access to important features. With the help of QSystemTrayIcon and QMenu, implementing a tray icon in python becomes straightforward.

I hope this blog post has given you an overview of how to create a tray icon using PyQt. Try it out and enhance your desktop application’s functionality and user-friendliness.