<>Python Realization of stopwatch , Display time, minutes and seconds , And there is a beginning , sign out , Reset button
from tkinter import * import time # Inherited from Frame Class of class Clock(Frame): def
__init__(self): Frame.__init__(self) self._start = 0.0 # private Start time set to 0 self.
_passtime= 0.0 # The time that has passed is set to 0 self._isRunning = False # Is the stopwatch running The default is No self.
timestr= StringVar() # Time string self.layout() # Graphical interface layout # layout def layout(self): lab =
Label(self, textvariable=self.timestr, font=100) self._setTime(self._passtime)
lab.pack(fill="x", expand=1, pady=20) # Set the time def _setTime(self, passTime):
minutes= int(passTime/60) seconds = int(passTime - minutes * 60.0) mseconds =
int((passTime - minutes * 60.0 - seconds) * 10) self.timestr.set(
'%02d:%02d.%01d' % (minutes, seconds, mseconds)) # Update time def _update(self): self.
_passtime= time.time() - self._start self._setTime(self._passtime) self.timer =
self.after(100, self._update) # each 100ms Update once # start def begin(self): if not self.
_isRunning: self._start = time.time() - self._passtime self._update() self.
_isRunning= True # stop it def stop(self): if self._isRunning: self.after_cancel(self
.timer) self._passtime = time.time() - self._start self._setTime(self._passtime)
self._isRunning = False # rebuild def reset(self): if not self._isRunning: #
Set only after stopwatch stops reset It works self._start = time.time() self._passtime = 0.0 self.
_setTime(self._passtime) if __name__ == '__main__': def main(): import tkinter
myclock= Tk() clk = Clock() width = 320 height = 96 screenwidth = myclock.
winfo_screenwidth() screenheight = myclock.winfo_screenheight() alignstr =
'%dx%d+%d+%d' % (width, height, (screenwidth-width)/2, (screenheight-height)/2)
myclock.geometry(alignstr) myclock.title("My Second Chronograph") clk.pack(side=
TOP) b1 = Button(myclock, text='Start', command=clk.begin, bg="#82A6F5") b1.pack
(fill="x", expand=1, side=LEFT) b2 = Button(myclock, text='Stop', command=clk.
stop, bg="#82A6F5") b2.pack(fill="x", expand=1, side=LEFT) b3 = Button(myclock,
text='Reset', command=clk.reset, bg="#82A6F5") b3.pack(fill="x", expand=1, side=
LEFT) b4 = Button(myclock, text='Quit', command=clk.quit, bg="#82A6F5") b4.pack(
fill="x", expand=1, side=LEFT) myclock.mainloop() main()
Technology