40

What's the simplest way to put a python script into the system tray?

My target platform is Windows. I don't want to see the 'cmd.exe' window.

Alterlife
  • 6,557
  • 7
  • 36
  • 49

1 Answers1

55

Those are two questions, actually:

  1. Adding a tray icon can be done with Win32 API. Example: SysTrayIcon.py
  2. Hiding the cmd.exe window is as easy as using pythonw.exe instead of python.exe to run your scripts.
Assaf Lavie
  • 73,079
  • 34
  • 148
  • 203
  • 3
    To run the SysTrayIcon demo it is necessary to put some icon files into the directory also. Search for *.ico under c:\windows and its subirectories and copy them. – mit Dec 22 '11 at 12:06
  • I tried this. It worked. but menu_item did not show image. Eg - ('Say Hello', icons.next(), hello) This do not show icon. – Durgaprasad Aug 17 '17 at 05:11
  • 2
    I modified `SysTrayIcon.py` to work with **Python 3** here: https://stackoverflow.com/a/48775936/2441026 – user136036 Feb 13 '18 at 21:43
  • The only issues with this program upgrading to Python3 is that print statements need to have parentheses, each occurrence of `icons.next` needs to be changed to `next(icons)` as per https://stackoverflow.com/questions/5237611/itertools-cycle-next, and `basestring` needs to be changed to `str` as per https://stackoverflow.com/questions/34803467/unexpected-exception-name-basestring-is-not-defined-when-invoking-ansible2 Then, the code should work as expected. – Pro Q Feb 23 '18 at 23:53