-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
107 lines (83 loc) · 2.78 KB
/
Copy pathmain.py
File metadata and controls
107 lines (83 loc) · 2.78 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from tkinter import Button, Tk, Label, Menu, Frame, mainloop
from time import strftime, strptime
from tkinter.constants import BOTTOM, X, SUNKEN, BOTH
class Clock(Tk):
def __init__(self):
super().__init__()
self.font = ("Terminal", 40)
self.title("Clock")
self.minsize(300, 80)
self.second = 0
self.minute = 0
self.draw()
def draw(self):
self.frame_btn = Frame(self)
self.toggle_btn = Button(
self.frame_btn,
text = "Open the Clock",
font = ("Digital", 14),
relief = SUNKEN,
bd = 3,
bg = "#02122c",
activebackground = "#02104a",
activeforeground = "#c7dbfb",
fg = "#c7dbfb",
command = self.toggle_clock
)
self.toggle_btn.pack(fill = BOTH, expand = True)
self.frame_btn.pack(side = BOTTOM, fill = BOTH, expand=True)
def toggle_clock(self):
self.frame_cl = Frame(self)
try :
self.frame1_sw.destroy()
except:
pass
self.clock_lbl = Label(
self.frame_cl,
text = self.return_time(),
font = self.font,
bg = "#02122c",
fg = "#c7dbfb"
)
self.clock_lbl.pack(fill = BOTH, expand = True)
self.frame_cl.pack(fill = BOTH, expand = True)
self.toggle_btn.config(text = "Switch to Stopwatch", command = self.toggle_stopwatch)
self.update_time()
def toggle_stopwatch(self):
self.frame1_sw = Frame(self)
try:
self.frame_cl.destroy()
except:
pass
self.sw_lbl = Label(
self.frame1_sw,
text = "00:00",
font = self.font,
bg = "#02122c",
fg = "#c7dbfb"
)
self.sw_lbl.pack(fill = BOTH, expand=True)
self.frame1_sw.pack(fill = BOTH, expand = True)
self.update_stopwatch()
self.toggle_btn.config(text = "Switch to Clock", command = self.toggle_clock)
def return_time(self):
return strftime("%H:%M:%S")
def update_stopwatch(self):
self.second += 1
if self.second % 60 == 0:
self.second = 0
self.minute += 1
try:
self.sw_lbl.config(text = f"{self.minute}:{self.second}")
except:
pass
self.after(1000, self.update_stopwatch)
def update_time(self):
try :
self.clock_lbl.configure(text = self.return_time())
self.after(200, self.update_time)
except:
pass
if __name__ == "__main__":
root = Clock()
root.mainloop()