Case Analysis of python_tkinter event types
Most people do not understand the knowledge points of this "python_tkinter event type case analysis" article, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "python_tkinter event type case analysis" article.
1. Event binding function
There are three event binding functions as follows:
Component .bind ('event type', event function)
Bind an operation for a component
Component .bind _ class ('component type', 'event type', event function)
Bind an operation for a class component
Component .bind _ all ('event type', event function)
Bind an operation for all components (all actions are treated as operations on the main interface)
two。 Event type
3. Event object
Case 1 of event binding:
The mouse turns red when you enter the component and turns white when you leave the component:
# single-line text input box entry = tkinter.Entry (root) entry.pack () # event function def changered (eventobj): # get the component eventobj.widget ['bg'] =' red'# mouse entry component red def wdc (eventobj): eventobj.widget ['bg'] =' white'# mouse leaves the component white # event binding entry.bind (', changered) entry.bind ('', wdc)
Note: through the binding function operation, the corresponding event function, there must be formal parameters to accept the event object.
Event functions must be defined before binding
Case 2 of event binding:
Keyboard shortcut trigger event: press the shortcut key to change the background color of the window
Import tkinter# create main window root = tkinter.Tk () # set window size root.minsize (300200) # turn red def changer (eventobj): eventobj.widget ['bg'] =' red'# turn red by Ctrl + r window: both commands R and r line root.bind (', changer) root.bind ('', changer) # join message loop root.mainloop ()
Case 3 of event binding: (binding events for a class of components)
Import tkinter# create main window root = tkinter.Tk () # set window size root.minsize (300200) # Button 1btn1 = tkinter.Button (root,text ='1') btn1.place (x = 20 tkinter.Button (root,text ='2') btn2.place (x = 80) btn2.place (x = 80)) # Button 3btn3 = tkinter.Button (root,text ='3') btn3.place Y = 20 5btn5 width = 40 Magi height = 40) # Button 4btn4 = tkinter.Button (root,text ='4') btn4.place (x = 20 Magi y = 80 Magi width = 40 Magi height = 40) # Button 5btn5 = tkinter.Button (root,text ='5') btn5.place (x = 80 Magi y = 80 Magi width = 40) # Button 6btn6 = tkinter.Button (root,text ='6') btn6.place (x = 140 btn6.place y = 80 Magi width = 40) Height = 40) def changebg (wdc): # the mouse on the button turns red wdc.widget ['bg'] =' red'def changebg1 (wdc): # the mouse leaves the button and the button turns white wdc.widget ['bg'] =' white'# bind button mouse entry event btn1.bind_class ('Button','',changebg) btn1.bind_class (' Button','',changebg1) # join message loop root.mainloop ()
Case 4 of event binding: (bind events for all components)
When you click on all the components, the background color of the Entry component will turn red:
Import tkinter# create main window root = tkinter.Tk () # set window size root.minsize (300200) # Button btn1 = tkinter.Button (root,text ='#') btn1.pack () # input box entry = tkinter.Entry (root) entry.pack () # Multiline input box text = tkinter.Text (root,width = 20 Height = 5) text.pack () # function def changeentry (e): # when the mouse clicks on any component, the Entry component will turn red entry ['bg'] =' red'# event binding btn1.bind_all ('', changeentry) # add message loop root.mainloop () these are the contents of the article "instance Analysis of python_tkinter event types" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.