Erram
Erram

Reputation: 35

Python pausing Threading

I'm really new to python and I don't even know if the title of this python question is even right. Anyways I'm facing an error code that I have been trying to find a fix for in the last couple of hours. The error goes like this: raise RuntimeError("threads can only be started once").

Now I googled the error and I got a couple of solutions. I tried a couple of them and none of them seem to work. I know that you cannot use the start function for more than one time, But how else am I supposed to get my script to work again?

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()

The error meesage:

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

Upvotes: 0

Views: 288

Answers (1)

Syed H
Syed H

Reputation: 270

A possible solution would be to intialize a new Listener each time before starting the Listener.

You could re-code your solution in the following way:

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()

Upvotes: 1

Related Questions