-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-tkinter-stopwatch.py
More file actions
46 lines (41 loc) · 1.62 KB
/
python-tkinter-stopwatch.py
File metadata and controls
46 lines (41 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
import tkinter as tk
import time
class digitalclock(tk.Frame):
def __init__(self, master=None, **kwords):
super().__init__(master, **kwords)
top = tk.Frame(self)
top.pack()
buttons = ("Start Stop Reset Quit")
for butn in buttons.split():
setattr(self, butn, butn)
action = lambda act=butn: self.showtime(act)
tk.Button(top, text=butn, command=action).pack(side="left")
self.lab = tk.Label(self, text="00:00:00", bg=self.cget("bg"))
self.lab.pack(side="bottom")
self.showtime(self.Reset)
def showtime(self, *args):
if args:
act = args[0]
if act == self.Quit:
self.run = False
self.quit()
elif act == self.Reset:
self.elapsed = 0.0
self.run = False
self.lab.config(text = "00:00:00")
elif act == self.Start and self.run == False:
self.starttime = time.time() - self.elapsed
self.run = True
elif act == self.Stop and self.run == True:
self.elapsed = time.time() - self.starttime
self.run = False
if self.run == True:
tim = int(time.time() - self.starttime)
hms = (tim // 3600, tim % 3600 // 60, tim % 60)
self.lab.config(text = '{:02d}:{:02d}:{:02d}'.format(*hms))
self.after(200,self.showtime)
root=tk.Tk()
colr = ["sky blue", "orange", "lightgreen", "yellow", "orchid1", "bisque2"]
[digitalclock(root, bd=8, bg=c).pack() for c in colr]
root.mainloop()