Skip to content

Instantly share code, notes, and snippets.

@hogelog
Created April 8, 2013 17:53
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save hogelog/5338905 to your computer and use it in GitHub Desktop.
Save hogelog/5338905 to your computer and use it in GitHub Desktop.
QSystemTrayIcon Example
#!/usr/bin/python
# Import PySide classes
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class App:
def __init__(self):
# Create a Qt application
self.app = QApplication(sys.argv)
icon = QIcon("jenkins_favicon.png")
menu = QMenu()
settingAction = menu.addAction("setting")
settingAction.triggered.connect(self.setting)
exitAction = menu.addAction("exit")
exitAction.triggered.connect(sys.exit)
self.tray = QSystemTrayIcon()
self.tray.setIcon(icon)
self.tray.setContextMenu(menu)
self.tray.show()
self.tray.setToolTip("unko!")
self.tray.showMessage("hoge", "moge")
self.tray.showMessage("fuga", "moge")
def run(self):
# Enter Qt application main loop
self.app.exec_()
sys.exit()
def setting(self):
self.dialog = QDialog()
self.dialog.setWindowTitle("Setting Dialog")
self.dialog.show()
if __name__ == "__main__":
app = App()
app.run()
@LelisThanos
Copy link

First of all, great working example, thank you for it.
Is there a way to keep the application running after activating QDialog? I'm trying to do something similar, but whenever i open a QDialog with some QInputDialogs and finish working on them, the QSystemTrayIcon does not respond to any further commands. Menu does not show with right click and i cannot close the application.

@saber-hyw
Copy link

python ???

@jannismain
Copy link

First of all, great working example, thank you for it.
Is there a way to keep the application running after activating QDialog? I'm trying to do something similar, but whenever i open a QDialog with some QInputDialogs and finish working on them, the QSystemTrayIcon does not respond to any further commands. Menu does not show with right click and i cannot close the application.

I'm having the same issue... Did you find a solution? (Using PyQt5 on macOS over here..)

@rhacs
Copy link

rhacs commented Sep 29, 2018

First of all, great working example, thank you for it.
Is there a way to keep the application running after activating QDialog? I'm trying to do something similar, but whenever i open a QDialog with some QInputDialogs and finish working on them, the QSystemTrayIcon does not respond to any further commands. Menu does not show with right click and i cannot close the application.

I'm having the same issue... Did you find a solution? (Using PyQt5 on macOS over here..)

import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (QApplication, QMainWindow, QMenu, 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)

        # SysTray Icon
        self.tray = QSystemTrayIcon(self)

        # Check if System supports STray icons
        if self.tray.isSystemTrayAvailable():
            self.tray.setIcon(self.windowIcon())

            # Context Menu
            ctmenu = QMenu()
            actionshow = ctmenu.addAction("Show/Hide")
            actionshow.triggered.connect(lambda: self.hide() if self.isVisible() else self.show())
            actionquit = ctmenu.addAction("Quit")
            actionquit.triggered.connect(self.close)

            self.tray.setContextMenu(ctmenu)
            self.tray.show()
        else:
            # Destroy unused var
            self.tray = None

        # Show App
        self.show()

if __name__ == "__main__":
    application = QApplication(sys.argv)
    application.setApplicationName("App Name")
    application.setApplicationVersion("1.1.1.1.1")
    application.setOrganizationName("dev name")

    win = App()
    sys.exit(application.exec_())

More info:

@jmason86
Copy link

jmason86 commented Oct 22, 2018

This worked for me just replacing PyQt5 with PySide2. This pops the icon up on the top menu bar on macOS. Related question: how do you set the application icon on the macOS dock?

@azzamsa
Copy link

azzamsa commented Dec 5, 2018

Thank you. It works great.

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