current position:Home>Analysis of Python event mechanism

Analysis of Python event mechanism

2022-01-30 06:53:46 Little cute in the circle of friends

Little knowledge , Great challenge ! This article is participating in “ A programmer must have a little knowledge ” Creative activities .


We were right ahead Python GUI-TKinter library (Grid、Place、Pack) Layout manager 、Canvas Components 、Checkbutton Components 、Radio button Components etc. , Write in the form of building blocks GUI Program

The graphical interface we learned earlier , But these interfaces are not yet responsive to any user action , For example, click the button in the window , This button does not provide any response .

This is because the program does not bind any event handling for this component .

In this issue , We will learn Python GUI Summary of event processing knowledge points ,Let's go~

1. Overview of event handling

One GUI The whole application life cycle is in a message cycle (event loop) in , It waits for the event to happen , And deal with it accordingly .

Tkinter Provides a mechanism for handling related events , The handler function can be bound to each event of each control .


 Copy code 

If the relevant event occurs ,handler The function will be triggered , Event object event Will pass to handler function .

2. Event binding mode

  1. command Binding method

Button(root,command= EVENT)
 Copy code 

command Binding mode features

  • The way is simple , Easy to use
  • The program cannot create a specific event ( For example, the mouse moves 、 Key events ) Binding event handling method
  • The program cannot get event related information
  1. bind() Method

bn = Button(root)

 Copy code 

bind() The way

  • The first parameter is event handling
  • The second parameter is event handling

3. Mouse and keyboard events

event explain
<Button-1> <ButtonPress-1> <1> Press the left mouse button .2 Represents the middle key ,3 Right click
<ButtonPelease-1> Release the left mouse button
<B1-Motion> Press and hold the left mouse button to move
<Double- Button-1> Double left click
<Enter> The mouse pointer enters a component area
<Leave> Mouse pointer to a component area
<MouseWheel> Scroll wheel
<KeyPress-a> Press down a key ,a You can use another key instead of
<KeyRelease-a> Release a key
<KeyPress-A> Press down A key ( uppercase A)
<Alt-KeyPress-a> Press at the same time alt and a,alt You can use ctrl and shift replace
<Double-KeyPress-a> Two quick clicks a
<Control-V> Ctrl and V The keys are pressed simultaneously ,V You can change to other key positions

event Object common properties

name explain
char Key characters , Valid only for keyboard events
keycode Key code , Valid only for keyboard events
keysym Key name , Valid only for keyboard events
num Mouse button , Valid only for mouse events
type The type of event triggered
widget The component that caused the event
width,height The size of the changed component , only Configure It works
x,y Current mouse position , Equivalent to parent container
x_root,y_root Current mouse position , Equivalent to the entire screen


Current pair GUI Two methods of event binding are used to summarize , I hope you are doing GUI When programming , Recommended bind() The way

The above is the content of this issue , Welcome big guys to praise and comment , See you next time ~

copyright notice
author[Little cute in the circle of friends],Please bring the original link to reprint, thank you.

Random recommended