Skip to content

Instantly share code, notes, and snippets.

@rhacs
Last active September 29, 2018 21:48
Show Gist options
  • Save rhacs/35c555169e6b259317bff7254261e4b2 to your computer and use it in GitHub Desktop.
Save rhacs/35c555169e6b259317bff7254261e4b2 to your computer and use it in GitHub Desktop.
[PyQt5] QSystemTrayIcon implementation example
import sys
# from PyQt5 import uic
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (QApplication, QMainWindow, QMenu, QPushButton, QSystemTrayIcon)
class App(QMainWindow):
def __init__(self, parent=None):
super(App, self).__init__(parent)
# Load UI from file
# uic.loadUi("app.ui", self)
# Or write your entire UI in code
self.setFixedSize(640, 480)
button = QPushButton('Show Message', self)
button.move(100, 200)
button.clicked.connect(self.show_message)
# SysTray Icon
self.tray = QSystemTrayIcon(self)
# Check if System supports tray icons
if self.tray.isSystemTrayAvailable():
self.tray.setIcon(self.windowIcon())
# Context Menu
ctmenu = QMenu()
action_show = ctmenu.addAction("Show / Hide")
action_show.triggered.connect(lambda: self.hide() if self.isVisible() else self.show())
action_msg = ctmenu.addAction("Show Message")
action_msg.triggered.connect(self.show_message)
action_quit = ctmenu.addAction("Quit")
action_quit.triggered.connect(self.close)
self.tray.setContextMenu(ctmenu)
self.tray.show()
else:
# Destroy unused var
self.tray = None
# Show App
self.show()
def show_message(self):
# Check if System supports messages
if self.tray != None and self.tray.supportsMessages():
self.tray.showMessage('Title', 'Message')
else:
print("Messages not supported")
if __name__ == "__main__":
application = QApplication(sys.argv)
application.setApplicationName("App Name")
application.setApplicationVersion("1.1.1.1.1")
application.setOrganizationName("dev name")
application.setStyle("fusion")
win = App()
sys.exit(application.exec_())
@rhacs
Copy link
Author

rhacs commented Sep 29, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment