I am trying to make it so that the user can use a hotkey to start/stop the program. I have a label to display what the hotkey is currently set to and a button to allow them to change it (I am using Pynput to detect keypresses).
The button is linked to the following code:
def set_Hotkey():
def on_press(key):
global hotkey
hotkey = key
listener.stop()
key_label.config(text=f'{key}')
def on_release(key):
pass
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
I have also tried using StringVar, and played around with setting the text to str(key)
instead of f'{key}'
all to no avail.
Whenever I run the program it starts up fine, and if I replace the key_label.config()
line with print(key)
or print(str(key))
it does it no bother. However, when I try and set the label text to display what key got pressed, the program freezes and I'm forced to close it down. I don't get any form of error message in my console, it just freezes.
Can someone please explain what's going on here and how to fix it?
pynput.keyboard.Listener()
is also a thread, so calling join()
will block tkinter
mainloop.
Change:
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
to:
listener = Listener(on_press=on_press, on_release=on_release)
listener.start()
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments