How can I change a tkinter label text to display key press information?

thisisminenow

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?

acw1668

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to change Tkinter label text on button press

Can I change the menubar text after press a button? (Python, tkinter)

How can i display the full text on a label?

How can I change the color of label with * in text

How can I change a label text after a specific time without after() function in tkinter python?

How can I change the color of text in Tkinter?

How do I change the text size in a label widget, python tkinter

How can I display larger text in small Label.text?

Python tkinter - Change the Label created by a loop, how can I do?

How to change text spacing for text in a Tkinter Label?

How can I display text AND variables in a tkinter text widget

How do I change a label's text to a file name on button press in Python?

How to set text to a label on keyboard key press in JavaFX application?

Xamarin How can i change label text color if it contains minus

How can I press the Windows key with xdotool

Tkinter label text changes with each button press

How do I clear the text in a Label with tkinter?

How can I tell bash top stop echoing text whenever I press any key?

How do I delete text when I press a button? (tkinter)

Can you display both an image and some text in a single tkinter label?

How can I wrap text in parenthesis in only one key press in VS Code?

How do I change the Text of A tkinter Label without pressing a Button in Python3?

Tkinter: How do I use the value of a scale-widget to change the text of a label-widget?

How can I display the contents of a variable that uses .sum() in a label using tkinter

How can i display a long string (paragraphs) in a label widget on multiple lines with a y scrollbar? (tkinter)

How can I change a label text without interfering with an input radio inside the label?

How to read every key press on TextInput irrespective of text change

How to get text from an OptionMenu when I press a button (Tkinter)

Tkinter, I want to change the text of a label, after generating it with a loop