Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Making Your Apps Interactive

00:00 Making Your Applications Interactive By now, you have a good idea of how to create a window with tkinter, add some widgets and control the application layout and that’s great, but applications shouldn’t just look good they actually need to do something.

00:16 In this section, you’ll learn how to bring your applications to life by performing actions whenever certain events occur. When you create a tkinter application, you must call window.mainloop() to start the event loop.

00:31 During the event loop your application checks if an event has occurred and if so, it will execute code in response to this. The event loop is provided for you by tkinter, so you don’t have to write any code that checks for events yourself.

00:46 However, you do have to write the code that will be executed in response to an event. In tkinter you write functions called event handlers for the events that you use in your application.

00:58 An event is any action that occurs during the event loop that might trigger some behavior in the application, such as when a key or mouse button is pressed.

01:07 When an event occurs, an event object is emitted, which means that an instance of a class representing the event is created. You don’t need to worry about instantiating these classes yourself.

01:18 tkinter will create instances of event classes for you automatically. First, you’ll take a look at an event loop to better understand how tkinter’s event loop works.

01:30 That way you’ll see how tkinter’s event loop fits into your application and which parts you’ll need to write yourself. Let’s assume there’s a list called events that contains event objects.

01:42 A new event object is automatically appended to events every time an event occurs in your program. You don’t need to implement this updating mechanism.

01:51 It automatically happens for you in this conceptual example. Using an infinite loop, you could continually check if there are any event objects in events.

02:02 Right now, this event loop doesn’t do anything with event, so let’s change that. Suppose your application needs to respond to key presses. You need to check that event was generated by a user pressing a key on their keyboard, and if so, pass event to an event handler function. For key presses, assume that event has a type attribute set to the string keypress.

02:25 If the event is a key press event object and a char attribute containing the character of the key that was pressed. You would create a new handle_keypress function and update the event loop code.

02:37 When you call window.mainloop(), something similar to this is run for you. This method takes care of the two parts of the loop. It maintains a list of events that have occurred and it runs an event handler anytime a new event is added to that list.

02:54 Let’s update this conceptual example to use real code and window.mainloop() instead of your own event loop.

03:12 mainloop() takes care of a lot for you, but there is something missing from this code. How does tkinter know when to use handle_keypress?

03:20 Fortunately, tkinter widgets have a method called .bind() for exactly this purpose.

03:28 To call an event handler. whenever an event occurs on a widget, you use .bind(). The event handler is said to be bound to the event because it’s called every time the event occurs.

03:40 You’ll continue with the key press example seen previously and use bind to bind handle_keypress to the key press event.

03:51 Here, the handle_keypress event handler is bound to a key event using window.bind(). Whenever a key is pressed, when the application is running, your program will print the character of the key that’s been pressed.

04:10 Note that the output is not printed in the tkinter application window. It’s printed to the standard output stream. If you are running this program in IDLE, then you’ll see the output in the interactive window.

04:21 If you run the program from a terminal as seen on screen, then you should see the output on your terminal.

04:30 bind() always takes at least two arguments. An event that’s represented by the string of the form seen on screen where event name can be any of tkinter’s events and an event handler that’s the name of the function to be called whenever the event occurs. The event handler is bound to the widget on which .bind() is called.

04:50 When the event handler is called, the event object is passed to the event handler function. Previously the event handler was bound to the window itself, but you can bind an event handler to any widget in your application.

05:05 For example, you could bind an event handler to a button widget that will perform some action whenever the button is pressed.

05:30 Here, the <Button-1> event on the button widget is bound to the handle_click event handler. The <Button-1> event occurs whenever the primary mouse button normally the left mouse button, is pressed while the mouse is over the widget.

05:46 There are other events for mouse button clicks including button two for the middle mouse button and button three for the secondary, or typically right mouse button.

06:02 Note that the window key press handler is also still working.

06:09 For a list of commonly used events, see the Events Type section of the tkinter reference at the link seen on screen. You can bind any event handler to any kind of widget with .bind(), but there’s a more straightforward way to bind event handlers to button clicks using the button widget’s command attribute and that’s what you’ll be looking at in the next section of the course.

Become a Member to join the conversation.