pythonpython-3.xtkinterpython-multithreadingpynput

Illegal Instruction:4 when using pynput keyboard with tkinter


I was trying to use pynput to create a automatic keyboard typer with tkinter. The pynput keyboard typer is made using a custom thread (note: I followed a certain tutorial that I can't find now). When executed by itself, it works fine, but when executed with tkinter, it creates a Illegal Instruction: 4 error.

Edit: as per @Karl's recommendation, I made a minimum example. The following code snippet re-creates the exact same issue:

import tkinter,threading,pynput,time
keyb = pynput.keyboard.Controller()
global thread_save
def typer(*args):
    while True:
        # print("spam")
        keyb.type("hi")
        time.sleep(0.1)
def beginning(*args):
    global thread_saved
    print("pressed")
    thread_saved = threading.Thread(target=typer)
    thread_saved.start()
    thread_saved.run()
root = tkinter.Tk()
but = tkinter.Button(text="Begin",master=root)
but.bind("<1>",beginning)
but.pack()
root.mainloop()

Expected Result: after clicking the button, the word"hi" will be typed over and over on the keyboard

Current Result: program crashes with Illegal Instruction:4

System version:

Macos 10.15.7

Intel Mac

packages:

pynput==1.7.6
pyobjc-core==10.0
pyobjc-framework-ApplicationServices==10.0
pyobjc-framework-Cocoa==10.0
pyobjc-framework-Quartz==10.0
six==1.16.0

Solution

  • After reading the comments, I'm still unable to resolve the problem via threads. However, I was sucessfull in using child processes.Thanks to @Tim Roberts for your comment.