sys.exit in python gives a SystemExit error pystray

573 Views Asked by At

I am running a PYSTRAY icon which starts a console program. When I try stopping it with this code

def stop_rtd():
    print("Stopping RTD")
    sys.exit()
    icon.stop()

The system throws back this error

An error occurred when calling message handler
Traceback (most recent call last):
  File "C:\Users\ramac\AppData\Local\Programs\Python\Python37\lib\site-packages\pystray\_win32.py", line 389, in _dispatcher
    uMsg, lambda w, l: 0)(wParam, lParam) or 0)
  File "C:\Users\ramac\AppData\Local\Programs\Python\Python37\lib\site-packages\pystray\_win32.py", line 209, in _on_notify
    descriptors[index - 1](self)
  File "C:\Users\ramac\AppData\Local\Programs\Python\Python37\lib\site-packages\pystray\_base.py", line 267, in inner
    callback(self)
  File "C:\Users\ramac\AppData\Local\Programs\Python\Python37\lib\site-packages\pystray\_base.py", line 368, in __call__
    return self._action(icon, self)
  File "C:\Users\ramac\AppData\Local\Programs\Python\Python37\lib\site-packages\pystray\_base.py", line 463, in wrapper0
    return action()
  File "C:/Tej/GitHub/Python/YahooDLs/try_systray.py", line 16, in stop_rtd
    sys.exit()
SystemExit

RTD is the console program which is perpetual. It stops but the icon continues and console does not close. On closing the console the program exits and the icon is closed.

I am running this on Windows10

Please help me solve this problem.

1

There are 1 best solutions below

0
On

I found an alternative way to exit the program. The os module also has a exit function called _exit. It takes one argument that is the exit code/status code. Usually it should be 0. So your function will look like this:

import os
def stop_rtd():
    icon.stop()
    os._exit(0)

Also note that you have to stop icon before exit the program.