Binding Keyboard Events with Tkinter – Python Tkinter GUI Tutorial #44

Posted by


In this tutorial, we will be discussing how to bind keyboard events with tKinter in Python. Keyboard events are used to track the keys pressed on the keyboard and perform certain actions based on the key pressed. By binding keyboard events with tKinter, you can create interactive GUI applications that respond to user input via keyboard.

To get started, make sure you have the tKinter library installed in your Python environment. If you don’t have it installed, you can do so by running the following command in your terminal:

pip install tk

Once you have tKinter installed, you can begin by creating a basic GUI window using the following code:

import tkinter as tk

root = tk.Tk()

root.mainloop()

Now that you have a basic GUI window, let’s proceed with binding keyboard events. There are two main keyboard events that you can bind with tKinter:

  1. Key Press Event: This event is triggered when a key is pressed on the keyboard.
  2. Key Release Event: This event is triggered when a key is released on the keyboard.

To bind a keyboard event with tKinter, you can use the bind method on the root window object. Here’s an example of how you can bind a key press event:

def on_key_press(event):
    print(f"Key Pressed: {event.keysym}")

root.bind("<KeyPress>", on_key_press)

In this example, we defined a function on_key_press that takes an event argument. The event.keysym property returns the key that was pressed on the keyboard. We then use the bind method to bind the <KeyPress> event to the on_key_press function.

Similarly, you can bind a key release event using the following code:

def on_key_release(event):
    print(f"Key Released: {event.keysym}")

root.bind("<KeyRelease>", on_key_release)

Now that you have bound the keyboard events, you can perform any desired actions within the event handling functions. For example, you can update a label in the GUI window based on the key pressed:

label = tk.Label(root, text="")
label.pack()

def on_key_press(event):
    label.config(text=f"Key Pressed: {event.keysym}")

root.bind("<KeyPress>", on_key_press)

In this example, we created a label widget and updated its text property within the on_key_press function to display the key that was pressed on the keyboard.

Overall, binding keyboard events with tKinter allows you to create interactive and responsive GUI applications that respond to user input via keyboard. With the examples provided in this tutorial, you can now effectively incorporate keyboard event handling in your Python tKinter applications.

0 0 votes
Article Rating
47 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@vedikajain3476
1 month ago

Gow is it possible to track keyboard and mouse event outside the tkinter window means if tkinter window is minimized and we are working on other screen window or application then it should auto detect that key is press or mouse is moved

@ronitmonga8043
1 month ago

Is it only me or this guy looks like Walter White from Breaking Bad?

@nutislucky4904
1 month ago

what you mean most mice don't have middle button anymore. All mice I have use has middle button.

@jhnsenteriments3747
1 month ago

sorry <Return> is not working

@jhnsenteriments3747
1 month ago

good sir all events are working only one <Return> is now working

@saiyed722-tp5sd
1 month ago

thank you sir

@GnuChanOS
1 month ago

this is 3 years old but what is this man i search tkinter keyboard event i watching button + keyboard event.? why button?

@shiulipervinurp
1 month ago

hi, I have designed a Entry forms in my desktop but in want to run it in a windows tablet too. But in input field it is not automaticaly offering a keyboard. How could I deal with the issue so that program with provide a keyboard only in tablet otherwise in desktop Normal keboard. thanks in advance

@omitbadgers5664
1 month ago

Thanks, are there any options to pass paramters into the binded function like when we call functions/methods normally?

@mirekblabla3041
1 month ago

How to use my own event instead of "<Button-1>"? I have a video. I go through video frames and perform some operations on the frames – after these operations are done, I want to update the image in tkinter window. How can I achieve that? Thanks.

@IceTheCoder
1 month ago

Thank you SO much.

@BentleyGadget
1 month ago

Can you bind more than one event to the button? Like, if you right-click, I bring up a help window? But, if you left-click, I bring up the data you submitted?

@maxwellhawk
1 month ago

Hey. I really need help.
I have few Labels that suppose to change color when u hover mouse over it.
I use bind bc I haven't find any other way to use widget like Label interactive, it also react on click, and do particular stuff.
My problem that when I create a function for changing color, I must make 2 functions – on is <enter> other <leave> and I have at least 3 labels, so it has to be 6 function. That looks very long and unprofessional.
I was trying just to run code like LabelName.configure(bg="red') instead of handler but it doesn't work. It accept only commands – function. And it pass in that function – event. So i was wonder is there a way to make 2 function and somehow pass in them name of the widget which pretty much an object than use it to execute color change code?

@mazenahmed1047
1 month ago

thanks so much you are a real lifesaver💙

@watwat7097
1 month ago

How do you control the detection rate ?

@salmanmallah1425
1 month ago

Man you clear my mind regarding to bind() function thanks alot INDEED YOU ARE A GOOD TEACHER!❤

@sumedhwarade1833
1 month ago

Hey John, can you please tell us about unbind part of this. I am trying to bind enter key <return> to multiple Entry widgets one after another. I am using barcode scanner and it auto-enter after scanning the code.

@andrewwalker1877
1 month ago

What if you want to be able to press the button or hit enter?

@kabir73722
1 month ago

Dear how can i do right click inside Entry (Textbox)?

@HypnosisBear
1 month ago

Hooo hoooo!!!! Man this video was very awesome and BTW I really liked the bind function! It's amazing!