Skip to content

Commit ba8ecdd

Browse files
committed
added stopwatch script
1 parent cd7106a commit ba8ecdd

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

GUI-Stopwatch/stopwatch.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
## tkinter and time modules are inbuilt
2+
from tkinter import *
3+
import time
4+
5+
def run():
6+
global root
7+
root = Tk()
8+
root.title("Stopwatch APP")
9+
width = 400
10+
height = 160
11+
screen_width = root.winfo_screenwidth()
12+
screen_height = root.winfo_screenheight()
13+
x = (screen_width / 2) - (width / 2)
14+
y = (screen_height / 2) - (height / 2)
15+
root.geometry("%dx%d+%d+%d" % (width, height, x, y))
16+
Top = Frame(root)
17+
Top.pack(side=TOP)
18+
stopWatch = StopWatch(root)
19+
stopWatch.pack(side=TOP,pady=15)
20+
Bottom = Frame(root, width=400)
21+
Bottom.pack(side=BOTTOM)
22+
Start = Button(Bottom, text='Start', command=stopWatch.Start, width=20, height=2, bg="green")
23+
Start.pack(side=LEFT)
24+
Stop = Button(Bottom, text='Stop', command=stopWatch.Stop, width=20, height=2, bg="red")
25+
Stop.pack(side=LEFT)
26+
Reset = Button(Bottom, text='Reset', command=stopWatch.Reset, width=15, height=2)
27+
Reset.pack(side=LEFT)
28+
root.config(bg="black")
29+
root.mainloop()
30+
31+
32+
class StopWatch(Frame):
33+
def __init__(self, parent=None, **kw):
34+
'''When object is initialized'''
35+
Frame.__init__(self, parent, kw)
36+
self.startTime = 0.0
37+
self.nextTime = 0.0
38+
self.onRunning = 0
39+
self.timestr = StringVar()
40+
self.MakeWidget()
41+
def Updater(self):
42+
'''update the time shown'''
43+
self.nextTime = time.time() - self.startTime
44+
self.SetTime(self.nextTime)
45+
self.timer = self.after(1, self.Updater)
46+
def MakeWidget(self):
47+
'''make widget for displaying time'''
48+
timeText = Label(self, textvariable=self.timestr, font=("callibri", 50), fg="yellow", bg="grey")
49+
self.SetTime(self.nextTime)
50+
timeText.pack(fill=X, expand=NO, pady=2, padx=2)
51+
def SetTime(self, nextElap):
52+
'''set time that is to be displayed'''
53+
mins = int(nextElap / 60)
54+
secs = int(nextElap - mins * 60.0)
55+
milisecs = int((nextElap - mins * 60.0 - secs) * 100)
56+
self.timestr.set('%02d:%02d:%02d' % (mins, secs, milisecs))
57+
def Start(self):
58+
'''start stopwatch'''
59+
if not self.onRunning:
60+
self.startTime = time.time() - self.nextTime
61+
self.Updater()
62+
self.onRunning = 1
63+
def Stop(self):
64+
'''stop stopwatch'''
65+
if self.onRunning:
66+
self.after_cancel(self.timer)
67+
self.nextTime = time.time() - self.startTime
68+
self.SetTime(self.nextTime)
69+
self.onRunning = 0
70+
def Reset(self):
71+
'''reset stopwatch'''
72+
self.startTime = time.time()
73+
self.nextTime = 0.0
74+
self.SetTime(self.nextTime)
75+
76+
77+
78+
79+
80+
if __name__ == '__main__':
81+
run()

0 commit comments

Comments
 (0)