#-*-coding:utf-8-*-' Created on November 25, 2015 @author: Zroad ' "from tkinter Import * Import time class stopwatch (Fra
Me): "" To implement a stopwatch widget "" "msec = def __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 () def makewidgets (self): "" "Production Time label" "L = label (self,textvariable = Self.ti MESTR) Self._settime (self._elapsedtime) l.pack (fill = X, expand = NO, Pady = 2, PADX = 2) def _update (self): "" "Update the label with the elapsed Time" "Self._elapsedtime = Time.time ()-Self._start self._settime (self._elapsed
Time) Self._timer = Self.after (self.msec,self._update) def _settime (Self,elap): "" "" "" "" To "" "" "Seconds: Hundred Minutes" "" minutes = Int (elap/60) seconds = Int (elap-minutes*60.0) hseconds = Int (Elap-minutes*60.0-seco NDS) *100) self.tImestr.set ('%02d:%2d:%02d '% (minutes,seconds,hseconds)) def start (self): "" "Start the stopwatch, and if it is started, ignore" "if not Self._running:self._start = Time.time ()-Self._elapsedtime self._update () self._runnin g = True def Stop (self): "" Stop stopwatch, if stopped, ignore "" "If Self._running:self.after_cancel (Self._tim ER) self._elapsedtime = time.time ()-Self._start self._settime (self._elapsedtime) sel f._running = False def reset (self): "" Reset Stopwatch "" "Self._start = Time.time () Self._elapsedtime = 0.0 self._settime (self._elapsedtime) If __name__ = = "__MAIN__": Def main (): root = Tk () SW =
Stopwatch (Root) sw.pack (side = top) Button (root, Text = ' start ', command = Sw.start). Pack (side = left) button (root, Text = ' Stop ', command = sw.stop). Pack (side = left) Button (root, Text = ' reset ', command = Sw.re
Set). Pack (side = left) Button (root, Text = ' quit ', command = root.quit). Pack (side = left) Root.mainloop () main ()
Implementation Effect diagram: