Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of python_tkinter event types

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report