3

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
  • Did you ever find a solution? I'm facing the same issue. Commented Mar 26, 2022 at 13:13

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.