7 Handling User Interactions
Tkinter Variables, Basic Widgets, and Handling User Interactions
Events Handling
An event handler must have a function that will determine which code executes when the event is triggered, this function is also known as a callback function
trace_add and trace_remove methods:
trace_add() method which helps to detect changes in variable values and perform actions accordingly, while trace_remove undos the tracking.
bind and unbind method
This method is essential for handling user interactions such as button clicks, mouse events, and keyboard inputs. It allows you to bind event handlers to specific widgets, making your application interactive and responsive to a specific kind of user interaction such as pressing a certain key on keyboard. It takes in 2 mandatory attribute which are the event sequence (enclosed in angled brackets) and a call back functions
We have various kinds of events that can be binded to a widget, and the following will be discussed
1. Mouse Events
2. Keyboard Events
3. Focus Events
4. Selection Event
5. Timer Events
Mouse Events
Mouse events in Tkinter allow you to respond to user interactions involving the mouse, such as clicking, hovering, and dragging. Possible mouse events include:
- <Button-1>: Left mouse button click.
- <Button-2>: Middle mouse button click.
- <Button-3>: Right mouse button click.
- <Double-Button-1>: Double-click with the left mouse button.
- <Enter>: Mouse cursor enters a widget.
- <Leave>: Mouse cursor leaves a widget.
- <Motion>: Mouse movement within a widget.
Keyboard Events
Keyboard events in Tkinter allow you to respond to user interactions involving keyboard inputs, such as key presses and releases. Possible keyboard events include:
- <Key>: Any key press.
- <KeyPress>: Also involves any key been pressed, however we can specify a particular key. For example, when we press “E” on keyboard you can rewrite as <KeyPress-E>, note that its case sensitive, thus is caplock is on just press the key “E”, else press Shift + “E”.
- <KeyRelease>: Triggers when any key been pressed is released, we can also specify a particular in the same principle as KeyPress.
- <Return>, <Enter>: Enter or Return key press.
- <Tab>: Tab key press.
- <Shift_L>, <Shift_R>, <Control_L>, <Control_R>, <Alt_L>, <Alt_R>: Left and right modifier keys (Shift, Control, Alt).
Focus Events
Focus events in Tkinter allow you to respond to changes in focus, such as when a widget gains or loses focus. For example, when you start typing into an entry field that’s a focus event and Possible focus events include:
- <FocusIn>: Widget gains focus.
- <FocusOut>: Widget loses focus.
Selection Event
Selection events in Tkinter allow you to respond to changes in text selection within widgets like entry fields and text boxes. Possible selection events include:
• <Selection>: Text selection changes.
• <SelectionRequest>, <SelectionClear>: Requests and clears for clipboard or X11
selection.
Timer Events
Timer events in Tkinter allow you to schedule tasks to be executed after a certain time delay.
Tkinter has no built in timer event. However, we can simulate timer-like behaviors using the
.after() method to schedule tasks to be executed after a certain delay in millisecond.
29
The project below show how to create a GUI that accurately display the current date and time.
The clock refreshes every 1000 millisecond ( second ). This functionality will be important if you need to create a larger project where you need to display current time or date Listing 4.1: Custom Icon for Your App, PP Style
30