Python暂停线程

2024-04-19 14:34:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python真的很陌生,我甚至不知道这个python问题的标题是否正确。无论如何,我面临着一个错误代码,我一直在努力寻找修复在过去的几个小时。错误如下:raiseruntimeerror(“线程只能启动一次”)

现在我在谷歌上搜索错误,找到了一些解决方案。我试了两个,但都不管用。我知道您不能多次使用start函数,但是我该如何让脚本重新工作呢

import pyautogui, requests, pyperclip
from pynput import mouse, keyboard
from tkinter import filedialog

url = "https://poxgur.com/"
file_path = filedialog.asksaveasfilename(defaultextension='.png')


def on_click(x, y, button, pressed):
    global currentMouseX, currentMouseY
    if pressed:
        currentMouseX, currentMouseY = x, y
    if not pressed:
        mouse_listener.stop()
        im = pyautogui.screenshot(region=(
            currentMouseX, currentMouseY, abs(currentMouseX - x), abs(currentMouseY - y)))
        im.save(file_path)
        print(file_path)
        files = {"file": open(file_path, "rb")}
        r = requests.post(url + "api.php", files=files)
        pyperclip.copy(url + r.text + ".png")
        # os.remove(file_path)
        mouse_listener.stop()
        return False


mouse_listener = mouse.Listener(on_click=on_click)


def on_scroll_lock_release(key):
    if key == keyboard.Key.scroll_lock:
        if not mouse_listener.is_alive():
            mouse_listener.start()


with keyboard.Listener(on_release=on_scroll_lock_release) as listener:
    listener.join()

错误提示:

Unhandled exception in listener callback
Traceback (most recent call last):
  File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\site-packages\pynput\_util\__init__.py", line 162, in inner
    return f(self, *args, **kwargs)
  File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\site-packages\pynput\keyboard\_win32.py", line 283, in _process
    self.on_release(key)
  File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\site-packages\pynput\_util\__init__.py", line 78, in inner
    if f(*args) is False:
  File "C:/Users/marti/Documents/PythonProjects/Acutal Projects/The 100 Projects/Screenshot2.0.py", line 33, in on_scroll_lock_release
    mouse_listener.start()
  File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\threading.py", line 848, in start
    raise RuntimeError("threads can only be started once")
RuntimeError: threads can only be started once
Traceback (most recent call last):
  File "C:/Users/marti/Documents/PythonProjects/Acutal Projects/The 100 Projects/Screenshot2.0.py", line 37, in <module>
    listener.join()
  File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\site-packages\pynput\_util\__init__.py", line 210, in join
    six.reraise(exc_type, exc_value, exc_traceback)
  File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\site-packages\six.py", line 702, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\site-packages\pynput\_util\__init__.py", line 162, in inner
    return f(self, *args, **kwargs)
  File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\site-packages\pynput\keyboard\_win32.py", line 283, in _process
    self.on_release(key)
  File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\site-packages\pynput\_util\__init__.py", line 78, in inner
    if f(*args) is False:
  File "C:/Users/marti/Documents/PythonProjects/Acutal Projects/The 100 Projects/Screenshot2.0.py", line 33, in on_scroll_lock_release
    mouse_listener.start()
  File "C:\Users\marti\anaconda3\envs\The 100 Projects\lib\threading.py", line 848, in start
    raise RuntimeError("threads can only be started once")
RuntimeError: threads can only be started once

Process finished with exit code 1

Tags: theinpyonliblineusersfile
1条回答
网友
1楼 · 发布于 2024-04-19 14:34:26

一个可能的解决方案是在每次启动侦听器之前初始化一个新的侦听器

您可以按以下方式重新编写解决方案:

mouse_listener = mouse.Listener(on_click=on_click)


def on_scroll_lock_release(key):
    if key == keyboard.Key.scroll_lock:
        if not mouse_listener.is_alive():
            mouse_listener = mouse.Listener(on_click=on_click)
            mouse_listener.start()

相关问题 更多 >