0

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
5
  • 1
    Please post the entire traceback message. its not immediately obvious where a thread is involved and the trace will help. Commented Jul 27, 2020 at 4:25
  • Will do, sorry! Commented Jul 27, 2020 at 4:27
  • I don't know pynput enough to know how to fix the problem, but mouse.Listener appears to inherit from Thread. Threads can only be started once but you are stopping and starting it on multiple events. I'm not sure the best way to solve the problem. Commented Jul 27, 2020 at 4:36
  • I know, but I cannot find another way of doing it. Commented Jul 27, 2020 at 4:40
  • You can try to recreate the mouse_listener inside on_scroll_lock_release(). Commented Jul 27, 2020 at 4:46

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This worked out great, **but i needed to make one adjusment ** which was that you should create a mouse listener parameter in the on_scroll_lock_release functiondef on_scroll_lock_release(key, mouse_listener= mouse.Listener(on_click=on_click)): , This fixed the code for me!
Oh yeah I had overlooked the fact the mouselistener was a global variable and couldn't have it's value set within a function. One way to make this possible is your way, that is passing it as a parameter to the function. Another efficient way is to just declare the variable as global mouselistener within the function before it's value is set.

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.