import sys
from PySide2.
QtGui import
QIcon
# from PyQt5.QtGui import QIcon
# from PyQt5.QtWidgets import QSystemTrayIcon, QAction, QMenu, QApplication, QWidget, qApp
def __init__(self, app):
super().__init__()
self.app = app
self.
setIcon(QIcon('icon.png')) self.activated.connect(self.icon_activated)
self.
show_action = QAction('&show') self.
quit_action = QAction('&exit') self.show_action.triggered.connect(app.show)
self.quit_action.triggered.connect(qApp.quit)
self.tray_menu.addAction(self.show_action)
self.tray_menu.addAction(self.quit_action)
self.setContextMenu(self.tray_menu)
def icon_activated(self, reason):
self.app.show()
# for Windows
self.app.activateWindow()
# for macOS
self.app.raise_()
def main():
app.setQuitOnLastWindowClosed(False)
# It works:
# my_tray_icon = TrayIcon(my_widget)
# my_tray_icon.show()
# But why doesn't this work? The icon does not appear in the system tray.
TrayIcon(my_widget).show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
import sys
from PySide2.QtGui import QIcon
from PySide2.QtWidgets import QSystemTrayIcon, QAction, QMenu, QApplication, QWidget, qApp
# from PyQt5.QtGui import QIcon
# from PyQt5.QtWidgets import QSystemTrayIcon, QAction, QMenu, QApplication, QWidget, qApp
class TrayIcon(QSystemTrayIcon):
def __init__(self, app):
super().__init__()
self.app = app
self.setIcon(QIcon('icon.png'))
self.activated.connect(self.icon_activated)
self.tray_menu = QMenu(QApplication.desktop())
self.show_action = QAction('&show')
self.quit_action = QAction('&exit')
self.show_action.triggered.connect(app.show)
self.quit_action.triggered.connect(qApp.quit)
self.tray_menu.addAction(self.show_action)
self.tray_menu.addAction(self.quit_action)
self.setContextMenu(self.tray_menu)
def icon_activated(self, reason):
if reason in (QSystemTrayIcon.Trigger, QSystemTrayIcon.DoubleClick):
self.app.show()
# for Windows
self.app.activateWindow()
# for macOS
self.app.raise_()
def main():
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
my_widget = QWidget()
# It works:
# my_tray_icon = TrayIcon(my_widget)
# my_tray_icon.show()
# But why doesn't this work? The icon does not appear in the system tray.
TrayIcon(my_widget).show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
To copy to clipboard, switch view to plain text mode
Bookmarks