Here are some examples of Tkinter interface updates, easy to understand and share.
from tkinter import Tk, Checkbutton, Labelfrom tkinter import StringVar, IntVarroot = Tk()text = StringVar()text.set(‘old‘)status = IntVar()def change(): if status.get() == 1: # if clicked text.set(‘new‘) else: text.set(‘old‘)cb = Checkbutton(root, variable=status, command=change)lb = Label(root, textvariable=text)cb.pack()lb.pack()root.mainloop()
From TkinterImport *Import timeClassStopWatch(Frame):"Implement a Stopwatch widget" ' msec =50Def__init__(Self, Parent=none, **kw): frame.__init__ (self, parent, kw) Self._start =0.0 self._elapsedtime =0.0 self._running =False self.timestr = Stringvar () self.makewidgets () Self.flag =TrueDefMakewidgets(self):' Make Time label ' L = label (self, textvariable = self.timestr) self._settime (self._elapsedtime) l.pack (fill = X, expand = NO, P Ady =2, PADX =2)Def_update(self): Self._elapsedtime = Time.time ()-Self._start self._settime (self._elapsedtime) Self._timer = Self.after ( Self.msec, Self._update)Def_settime(Self, Elap):"' Change the time format to minutes: seconds: hundred seconds ' minutes = Int (elap/seconds = Int (elap-minutes*60.0) hseconds = Int (elap-minutes*60.0-seconds) *(Self.timestr.set) ('%2d:%2d:%2d '% (minutes, seconds, Hseconds))DefStart(self):IfNot Self._running:self._start = Time.time ()-Self._elapsedtime self._update () self._running =TrueDefStop(self):"Stop the Stopwatch"If Self._running:self.after_cancel (self._timer) self._elapsedtime = Time.time ()-Self._start self._setTime (self._ ElapsedTime) self._running =FalseDefReset(self):"Reset Stopwatch" Self._start = Time.time () Self._elapsedtime =0.0 self._settime (Self._elapsedtime)DefStopwatch(self):if Self.flag = =True:self.pack (side = TOP) Button (self, Text =' Start ', command = self. Start). Pack (side = left) Button (self, Text =' Stop ', command = self. Stop). Pack (side = left) Button (self, Text =' Reset ', command = self. Reset). Pack (side = left) Button (self, Text =' Quit ', command = self.quit). Pack (side = left) Self.flag =FalseClassWatch(Frame): msec =1000Def__init__(Self, Parent=none, **kw): frame.__init__ (self, parent, kw) Self._running =False self.timestr1 = Stringvar () self.timestr2 = Stringvar () self.makewidgets () Self.flag =TrueDefMakewidgets(self): L1 = label (self, textvariable = self.timestr1) L2 = label (self, textvariable = self.timestr2) l1.pack () L2.pack ()Def_update(self): self._settime () Self.timer = Self.after (self.msec, Self._update)Def_settime(self): Today1 = str (Time.strftime ('%y-%m-%d ', Time.localtime (Time.time ())) time1 = str (Time.strftime ( '%h:%m:%s ', Time.localtime (Time.time ()))) Self.timestr1.set (today1) self.timestr2.set (time1) def Span class= "Hljs-title" >startif __name__ = = ' __main__ ': def main (): root = Tk () root.geometry ( ' 250x150 ') frame1 = Frame (root) frame1.pack (side = BOTTOM) SW = StopWatch (root) Stpwtch = Button (frame1, text = "stopwatch", command = Sw.stopwatch) stpwtch.pack (side = right) MW = Watch (root) Mywatch = button (fr ame1, Text = ' clock ', command = Mw.start) Mywatch.pack (side = left) Root.mainloop () main ()
Python tkinter Control update information