Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.
preface
We have already written GUI programs as building blocks for Python guI-Tkinter libraries (Grid, Place, Pack) layout manager, Canvas component, Checkbutton component, Radio Button component, etc
The graphical interfaces we studied earlier are not yet responsive to any user action, such as clicking a button in a window that does not provide any response.
This is because the program does not bind any event handling to this component.
In this installment, we’ll learn about Python GUI event handling: Let’s Go ~
1. Event handling Overview
The entire life cycle of a GUI application resides in an event loop, which waits for events to occur and processes them accordingly.
Tkinter provides a mechanism for handling related events, and handlers can be bound to individual events of individual controls.
widget.bind(event,handler)
Copy the code
If an event occurs, the handler function is fired, and the event object is passed to the handler function.
2. Event binding mode
- Command binding method
The Button (root, the command = EVENT)Copy the code
Features of the Command binding mode
- The way is simple and easy to use
- The program cannot bind event handling methods to specific events, such as mouse movement or keystroke events
- The program could not obtain information about the event
- The bind () method
bn = Button(root)
bn.bind('<Double-1>',EVENT)
Copy the code
The bind () method
- The first parameter is event handling
- The second parameter is event handling
3. Mouse and keyboard events
The event | instructions |
---|---|
<Button-1> <ButtonPress-1> < 1 > |
Press the left mouse button. 2 indicates the middle key, and 3 indicates the right key |
<ButtonPelease-1> |
Release the left mouse button |
<B1-Motion> |
Press and hold the left mouse button to move |
<Double- Button-1> |
Double click |
<Enter> |
The mouse pointer goes to a component area |
<Leave> |
Mouse pointer to a component area |
<MouseWheel> |
Scroll wheel |
<KeyPress-a> |
When you press a, A can be replaced by another key |
<KeyRelease-a> |
The release of a key |
<KeyPress-A> |
Press the A key (capital A) |
<Alt-KeyPress-a> |
Press Alt and A at the same time. Use CTRL and Shift instead of Alt |
<Double-KeyPress-a> |
Two quick clicks on a |
<Control-V> |
Ctrl and V keys are pressed at the same time, and V can be replaced with another key |
Common properties of the Event object
The name of the | instructions |
---|---|
char | Key character, valid only for keyboard events |
keycode | Key encoding, only for keyboard events |
keysym | Key name, valid only for keyboard events |
num | Mouse button, only for mouse events |
type | The type of event that is triggered |
widget | The component that causes the event |
width,height | The size of the component changed, valid only for Configure |
x,y | The current position of the mouse corresponds to the parent container |
x_root,y_root | The current position of the mouse is equivalent to the entire screen |
conclusion
This article summarizes two ways to bind GUI events. We recommend using bind() for GUI programming
That’s the content of this episode. Please give us your thumbs up and comments. See you next time