Tkinter realizes stopwatch function

Source: Internet
Author: User
#-*-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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.