运行时错误:无法在解释器关闭时创建新线程。 Python 3.12

问题描述 投票:0回答:1

我正在制作一个鼠标抖动器,一旦脚本中的循环启动,我就无法访问托盘图标。我又问了一个问题,但还没有人回答,所以我继续深入研究这个话题以解决我的问题。所以,我找到了一个我认为的解决方案——线程。我有点理解我找到的代码,但不完全理解,所以现在的问题可能是我的理解而不是代码本身。

我发现不久前发布了同样的问题和同样的错误,但没有答案。所以,我可能认为 Python 3.12 中的 threading.py 有一个错误 - 我不知道。

这是我的代码:

from PIL import Image
import pyautogui
import time
import pystray
import threading
import os

class MyClass:
    def __init__(self):
        self.__running = False
        self.__stop_event = threading.Event()
        
    def run(self):
        while not self.__stop_event.is_set():
            pyautogui.moveRel(50, 0, duration = 0)
            pyautogui.moveRel(-50,0, duration = 0)
            time.sleep(5)
            print("running")
                
    def change_running_state(self):
        print(f"Running: {self.__running}")
        self.__running = not self.__running
        print(f"Running: {self.__running}")
        
        if self.__running:
            self.__stop_event.clear()
            t = threading.Thread(target=self.run)
            t.start()
        else:
            self.__stop_event.set()
if __name__ == "__main__":
    def start(icon, item):
            print("start")
            cl.change_running_state()

    def stop(icon, item):
        print("stop")
        cl.change_running_state()

    def exit_program(icon, item):
        print("exit")
        cl.change_running_state()
        icon.stop()
        os._exit(1)

image = Image.open("macos.jpg")

cl = MyClass()
icon = pystray.Icon("macos", image)
icon.menu=pystray.Menu(
    pystray.MenuItem("Start", start),
    pystray.MenuItem("Stop", stop),
    pystray.MenuItem("Exit", exit_program),
)
icon.run_detached()

现在只要我单击该图标,托盘图标就可以工作并且菜单项就会出现在那里。我想,菜单项也应该有效。相反,我得到以下信息:

start
Running: False
Running: True
An error occurred when calling message handler
Traceback (most recent call last):
  File "C:\Users\derby\AppData\Local\Programs\Python\Python312\Lib\site-packages\pystray\_win32.py", line 412, in _dispatcher
    return int(icon._message_handlers.get(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\derby\AppData\Local\Programs\Python\Python312\Lib\site-packages\pystray\_win32.py", line 224, in _on_notify 
    descriptors[index - 1](self)
  File "C:\Users\derby\AppData\Local\Programs\Python\Python312\Lib\site-packages\pystray\_base.py", line 328, in inner       
    callback(self)
  File "C:\Users\derby\AppData\Local\Programs\Python\Python312\Lib\site-packages\pystray\_base.py", line 453, in __call__
    return self._action(icon, self)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\derby\PythonWorkspace\Mouse Jiggler\Jiggler copy.py", line 34, in start
    cl.change_running_state()
  File "c:\Users\derby\PythonWorkspace\Mouse Jiggler\Jiggler copy.py", line 28, in change_running_state
    t.start()
  File "C:\Users\derby\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 971, in start
    _start_new_thread(self._bootstrap, ())
RuntimeError: can't create new thread at interpreter shutdown

那么这是怎么回事?经过 6 个小时的编码后,我肯定会很累,但对于我这个新手程序员来说,自己还是很难理解这一点。

python python-3.x multithreading runtime-error pystray
1个回答
0
投票

你可能想检查这个github问题,似乎python在

3.12

之后添加了一个检查
© www.soinside.com 2019 - 2024. All rights reserved.