Search code examples
python-3.xmacospyinstallersystem-traypyside6

I don't see icon on macOS system tray with the executable generated by pyinstaller


I develop an application with an icon in the system tray of macOS but when I generate the executable with pyinstaller and I launch the application, the menu is well in the system tray but I do not see the icon, as if I had nothing.

I use PySide6 6.4.0.1, Python 3.10.7 and pyinstaller 5.6.1. Here is the code for the system tray

app = QApplication()
main_window = MainWindow()
if not QSystemTrayIcon.isSystemTrayAvailable():
    QMessageBox.critical(None, 'System Tray', 'System tray was not detected!')
    sys.exit(1)

app.setQuitOnLastWindowClosed(False)

icon = QIcon('ressources/icon.jpg')

tray = QSystemTrayIcon()
tray.setIcon(icon)
tray.setVisible(True)

menu = QMenu()

action_hello = QAction('Hello World')
action_hello.triggered.connect(main_window.say_hello)
menu.addAction(action_hello)

action_exit = QAction('Exit')
action_exit.triggered.connect(app.exit)
menu.addAction(action_exit)

tray.setToolTip('Hello World app')
tray.setContextMenu(menu)
tray.show()

# Launch the app
app.exec()

Solution

  • I finally succeeded. I have found a part of the solution here! It was necessary to add the icon in a resource file specific to Qt. Here is a tutoriel to do that. I have create a file icon.qrc :

    <!DOCTYPE RCC><RCC version="1.0">
    <qresource prefix="/">
       <file>ressources/icon.ico</file>
    </qresource>
    </RCC>
    

    Then, I have run the following commmand in cmd : pyside6-rcc icons.qrc -o rc_icons.py. After, I I imported the new file created rc_icons.py in hello.py : import rc_icons.py. Finally, I have created my icon like this : icon = QIcon(QPixmap(":/ressources/icon.ico")) Now, when I run the pyinstaller command and the executable, I see the icon on the system tray.