python-3.x - python 中的 sys.exit 给出一个 SystemExit 错误

我正在运行一个启动控制台程序的 PYSTRAY 图标。当我尝试用这段代码停止它时

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

系统抛出这个错误

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 是永久的控制台程序。它停止但图标继续并且控制台不关闭。关闭控制台时,程序退出并且图标关闭。

我在 Windows10 上运行这个

请帮我解决这个问题。

最佳答案

我找到了退出程序的替代方法。 os 模块还有一个名为 _exit 的退出函数。它接受一个参数,即退出代码/状态代码。通常它应该是0。所以你的函数看起来像这样:

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

另请注意,您必须在退出程序之前停止 icon

关于python-3.x - python 中的 sys.exit 给出一个 SystemExit 错误 pystray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64457320/

相关文章:

javascript - Windows Localhost 中的 Wiki.js 安装

dart - Dart 中的不同流

javascript - 从 MapBox GL JS 中的聚类中排除某些功能

reactjs - 如何更改点击 react 的类(class)?

reactjs - 如何始终覆盖具有不确定后缀的 JSS 样式?

hyperledger-fabric - super 账本结构 2.0 : Chaincode co

ansible - Ansible 中角色的串行执行

ios - 带有 iOS 14 设备的 Xcode 10 - 尝试与此设备通信时遇到错误。 (该服务

r - 以编程方式将 R Shiny 应用程序作为后台作业启动

firebase - 使用 Mockito 为 Firebase 用户身份验证设置单元测试