In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will introduce you in detail "example analysis of event handling in Python Tkinter", the content steps are clear and detailed, and the details are handled properly, while the editor updates different knowledge points every day. I hope this "example analysis of event handling in Python Tkinter" can give you unexpected gains. Please follow the editor's ideas slowly and deeply, as follows, let's go to harvest new knowledge.
Event handling is an indispensable and important part of GUI programs. Comparatively speaking, controls are only components that make up a machine, while event handling is the key to driving the "normal" operation of this machine. It can run through the parts "elegantly". Therefore, "event handling" is the "soul" of GUI programs, and it is also the key to the realization of human-computer interaction.
The term "event" is occasionally mentioned when explaining controls, and in this section we will describe the event handling mechanism in Tkinter in more detail.
In a GUI program, users' actions to the software are collectively referred to as "events", such as mouse clicks, keyboard input text and window manager-triggered redrawing events. These events have a common feature, that is, they are triggered directly or indirectly by the user.
Event binding method
The event handling mechanism provided by Tkinter allows us to bind corresponding events and event handling functions (that is, callback functions) for the "control", thus realizing the interaction between the control and the user. The syntax format is as follows:
Widget.bind ("", func)
In the above syntax, widget represents the instance object of the control, followed by event binding using the bind () method, which takes two parameters:
A string parameter that indicates the type of event and is wrapped in the form of "angle brackets"
Func: represents the event handler (callback, that is, the callback function). When an event is triggered, Tk carries an event object (Event) to call the func method.
Note: the bind () method binds the event to the handler, while the unbind () method unbinds the event to the handler.
Common event types
The event type (also known as event code) is specified by the Tkinter module, which mainly includes mouse, keyboard, cursor and other related events. Tkinter defines the corresponding syntax format for it:
The above grammar consists of three parts, which are explained as follows:
The event type must be contained in angle brackets
Modifier: optional, modifier of event type, usually used to describe key combinations, double-click, uppercase lock keys, etc.
Type: an essential item that indicates the specific type of event
Detail: optional, usually used to describe which button is specific, such as the left mouse button
It is necessary to give a brief introduction to the frequently used modifier modifiers, which can modify the activation conditions of an event, such as double-clicking the mouse or pressing a key at the same time to trigger the event. The commonly used modifiers are as follows:
Modifier indicates that you need to press the Control key when the Control event occurs, the Alt key needs to be pressed when the Alt event occurs, the Shift key needs to be pressed when the Shift event occurs, the Lock event needs to be in uppercase lock state, the Double event occurs twice in a row, for example, the double-click mouse Triple event occurs three times in a row and the Quadruple event occurs four times in a row.
The event types commonly used in Tkinter are described in the following table, as follows:
The event code describes clicking the left mouse button, abbreviated as, the following number can be 1-2-3, which represents the left button, the middle pulley and the right button to release the left mouse button, respectively, and the following number can be 1-2-3. They represent the release of the left button, the pulley, the right button and the movement of the left mouse button. And means holding down the mouse pulley, right moving, turning the mouse pulley, double-clicking the left mouse cursor to enter the control instance, leaving the mouse cursor away from the control instance, pressing any key on the keyboard / pressing a letter or numeric key on the keyboard to release the key enter key on the keyboard, other similar keys are / / spacebar / / direction keys. Commonly used function key combinations, for example, indicate that the user clicks Ctrl + Shift + T at the same time when the control gets focus, such as mouse click to input the control input, you can call the focus_set () method to make the control gain focus and activate when the control loses focus, such as triggering an event when the control changes when the mouse leaves the input box For example, the Event event is triggered when the state of the control changes from "active" to "inactive" when the state of the control changes from "active" to "inactive". When the control is destroyed, the function that triggers the event triggers the event when some part of the window or component is no longer overwritten.
When the event is triggered, Tkinter automatically hands over the event object to the callback function for next processing. The Event object contains the following common properties:
Property describes which control XMagi y is responsible for the widget event relative to the upper left corner of the window, the current mouse coordinate position relative to the upper left corner of the screen, the current mouse coordinate position char is used to display the character keysym key name corresponding to the pressed key, for example, Control_L represents the left Ctrl key keycode key code, the number of a key, for example, the Delete key code is one of the 107num1/2/3 Indicates which button of the mouse was clicked. The button is divided into the modified size of the left, middle and right width,height control, corresponding to the event type event type.
Here is a set of examples of the use of "keyboard events":
From tkinter import * # defines event functions You must use the event parameter def show_key (event): # to view the button that triggered the event s=event.keysym # to display it on the button control lb.config (text=s) root=Tk () root.config (bg='#87CEEB') root.title ("C language Chinese website") root.geometry ('450x350 / Chinese 200') root.iconbitmap (' C:/Users/Administrator/Desktop/ C Chinese logo.ico') # add a button control lb=Label (root) Text=' Please press', fg='blue',font= ('Microsoft Acer', 15)) # bind events to button controls Press any key and call the event handler. Note that you need to enter lb.bind ('', show_key) # set the button to get focus lb.focus_set () lb.pack () # display window root.mainloop ()
The running result of the program is as follows:
Figure 1:Tkinter event handling
Note: in the above example, keyboard events can be received only after the Label control has acquired focus, so after binding events and callback functions to the control, you need to use the focus_set () method to get focus.
Let's take a look at a set of related examples of "mouse events":
# define the event function from tkinter import * def handleMotion (event): lb1 ['text'] =' you moved the cursor position 'lb2 [' text'] = 'current cursor position: X =' + str (event.x) +' Y='+str (event.y) print ('cursor current position', event.x,event.y) # create the main window win = Tk () win.config (bg='#87CEEB') win.title ("C language Chinese net") win.geometry ('450x350') win.iconbitmap ('C:/Users/Administrator/Desktop/ C Chinese net logo.ico') # create a form container frameframe = Frame (win, relief=RAISED, borderwidth=2, width=300,height=200) frame.bind ('' HandleMotion) lb1 = Label (frame,text=' does not have any event triggers', bg='purple',) # use place for location layout In the next section, we will introduce lb1.place (Xero20Magne20) lb2 = Label (frame,text='') lb2.place (Xero16Power60) frame.pack (side=TOP) # display window win.mainloop ()
The running result of the program is as follows:
Figure 2:Tkinter mouse movement event
What are the data types of python? the data type of python: 1. Numeric types, including int (integer), long (long integer), and float (floating point). two。 String, which is of type str and type unicode, respectively. 3. Boolean, Python Boolean is also used for logical operations and has two values: True (true) and False (false). 4. List, the list is the most frequently used data type in Python, and any data type can be placed in the collection. 5. Tuples are identified by "()", and internal elements are separated by commas. 6. A dictionary is a collection of key-value pairs. 7. A collection is an unordered, non-repeating combination of data.
Thank you for reading here. The editor hopes that you will have the most profound understanding of the key issue of "sample Analysis of event handling in Python Tkinter" from the practical level, and the specific usage needs to be used by everyone. If you want to read more related articles, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.