Adding file to System Tray
Question:
I have created an .exe file. Now I want it to run on startup as a background app. I don’t want it to display on taskbar, only on the system tray such that when you double-click the icon on the system tray. the file opens up.
Context: This is a socket client program which will connect to the server on startup and once a request is received it will get the print jobs, download the file and print the file. The program runs on terminal window and displays info like the print jobs, status of the print, etc. I want the user to be able to view this terminal when they double click the icon in system tray. Once they open the app from the tray and close it, it must still be in the tray. The only way to close it must be to right click the icon and pressing Exit.
I tried searching for ways to implement it even used chatgpt but still no clear cut answer.
Answers:
As a starting point, there’s 2 ways that I know of.
- Via the Windows API:
This is very low level, and I don’t have the knowledge to help out. However I did attempt it once and got a somewhat working implementation that can show/hide a console window: tray.py
- Using the PySide/PyQt libraries.
You can create a QtWidgets.QSystemTrayIcon
instance and attach it to a window. In the window, you override closeEvent
to ignore the event and hide the window, optionally telling the QSystemTrayIcon to display a notification that your window has been minimised to the tray.
This way would be a lot easier, but you may need to design Qt window that’ll act as the console for it to work properly. It would be possible update a widget with the output from stdout
if you wanted to go down that route.
I have created an .exe file. Now I want it to run on startup as a background app. I don’t want it to display on taskbar, only on the system tray such that when you double-click the icon on the system tray. the file opens up.
Context: This is a socket client program which will connect to the server on startup and once a request is received it will get the print jobs, download the file and print the file. The program runs on terminal window and displays info like the print jobs, status of the print, etc. I want the user to be able to view this terminal when they double click the icon in system tray. Once they open the app from the tray and close it, it must still be in the tray. The only way to close it must be to right click the icon and pressing Exit.
I tried searching for ways to implement it even used chatgpt but still no clear cut answer.
As a starting point, there’s 2 ways that I know of.
- Via the Windows API:
This is very low level, and I don’t have the knowledge to help out. However I did attempt it once and got a somewhat working implementation that can show/hide a console window: tray.py
- Using the PySide/PyQt libraries.
You can create a QtWidgets.QSystemTrayIcon
instance and attach it to a window. In the window, you override closeEvent
to ignore the event and hide the window, optionally telling the QSystemTrayIcon to display a notification that your window has been minimised to the tray.
This way would be a lot easier, but you may need to design Qt window that’ll act as the console for it to work properly. It would be possible update a widget with the output from stdout
if you wanted to go down that route.