In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to define and use your own timer in Tkinter". In daily operation, I believe many people have doubts about how to define and use their own timer in Tkinter. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "how to define and use your own timer in Tkinter". Next, please follow the editor to study!
In addition to accepting user actions and providing necessary feedback, GUI programs often need to do some processing on their own. For most development languages, introducing a timer is usually the easiest option.
The following code is a reusable Timer class implemented using the after function of the Tkinter control.
Class Timer: def _ init__ (self, wnd, ms, call): self.__wnd = wnd self.__ms = ms self.__call = call self.__running = False
Def start (self): if not self.__running: self.__wnd.after (0, self.__on_timer) self.__running = True
Def stop (self): if self.__running: self.__running = False
Def is_running (self): return self.__running
Def _ on_timer (self): if self.__running: self.__call () self.__wnd.after (self.__ms, self.__on_timer)
The user can specify the window object, the timing time and the processing to be performed when the Timer object is initialized. When the user calls the start method to start the timer, the timer class calls the after method of the window object with its own _ _ on_timer method as a parameter. When the timing time is up, the _ _ on_timer method is called, and the processing specified by the user through the initialization method is first executed, and then the after method that starts the window is called again and the _ _ on_timer method is triggered after the timing time. As long as _ _ running is True,__on_timer method, it will be triggered again and again until the user calls the stop method.
Let's take a digital clock as an example to introduce the use of this Timer class:
Class DigitalClock: def _ init__ (self, canvas, width, height): self.canvas = canvas self.width = width self.height = height # create font ftDate = Font (family='Times', size=32) self.canvas.create_text (width / 2, height / 4, text='', font=ftDate Tag='date') ftTime = Font (family='Times', size=64) self.canvas.create_text (width / 2, height / 2, text='', font=ftTime, tag='time')
Def update (self): time_str = time.strftime ('% Y.%m.%d% axiom, time.localtime ()) self.canvas.itemconfigure ('date', text=time_str) time_str = time.strftime ('% HV% MV% time.strftime, time.localtime ()) self.canvas.itemconfigure ('time', text=time_str)
DigitalClock builds two Text objects, one large and one small, to display time and date information during initialization. When the user calls the update method, DigitalClock gets the system time and updates the contents of the two Text objects.
The next main program is simple:
# create the main windowroot = Tk () # create canvascanvas = Canvas (root, height= 400,400) canvas.grid (row=0, column=0) clock = DigitalClock (canvas, 400400) timer = Timer (root, 1000, clock.update) timer.start () root.mainloop () timer.stop ()
Just build the clock class, the timer class and specify the update method of the clock class as the processing parameter of the timer class.
At this point, the study on "how to define and use your own timer in Tkinter" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.