-
Notifications
You must be signed in to change notification settings - Fork 3
/
UI.py
303 lines (247 loc) · 10.7 KB
/
UI.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
from tkinter import *
from tkinter import ttk
from Function import *
# create interface
class UI:
def __init__(self, root, minimize_helper):
root.title("Clock")
root.geometry("900x450")
self.root = root
#define flag
self.is_starting = False
self.stop_timer = False
self.stop_flag = False
self.sp_start_flag = False
self.is_moving = False
# define font
# seem not to work('TclError' object has no attribute 'message')
#self.roboto_mono = Font(file="Assets\\RobotoMono-Regular.ttf", family="Roboto Mono")
# Timer Tab_UI
self.minutes = StringVar()
self.second = StringVar()
self.sw_minute = StringVar()
self.sw_second = StringVar()
self.sw_centi = StringVar()
self.theme_value = BooleanVar()
self.lap_1=StringVar()
self.lap_2=StringVar()
self.lap_3=StringVar()
self.lap_4=StringVar()
self.lap_5=StringVar()
#create notebook
self.notebook = ttk.Notebook(root)
self.notebook.pack(fill=BOTH, expand=True)
# custom title bar
logo_frame = Frame()
self.notebook.add(logo_frame, text=" ")
self.logo =Label(self.notebook, compound=LEFT)
self.logo.place(x=10, y=10)
self.title_frame = Frame(self.notebook, relief="flat", border=0)
self.title_frame.pack(side=TOP, anchor=E, pady=8)
self.close = Button(self.title_frame, text='',
relief='flat',
border=0,
width=30,
command=lambda: self.close_win(root))
self.close.grid(row=0, column=2)
self.minimize = Button(self.title_frame, text='',
relief='flat',
border=0, width=30,
command=lambda: minimize_window(root, minimize_helper))
self.minimize.grid(row=0, column=0)
self.maximize = Button(self.title_frame, text='',
relief='flat',
border=0, width=30,
command=lambda: maximize_win(self,root))
self.maximize.grid(row=0, column=1)
# create notebook tab
tab1 = Frame(self.notebook)
self.notebook.add(tab1, text="Timer", compound=LEFT)
# create stopwatch tab
self.tab2 = Frame(self.notebook)
self.notebook.add(self.tab2, text="Stop Watch",
compound=LEFT)
self.notebook.select(tab1)
self.notebook.tab(0, state="disabled")
# first half frame
tab1_firstFm = Frame(tab1)
tab1_firstFm.pack(fill="both", expand=True)
# second half frame
tab1_secondFm = Frame(tab1)
tab1_secondFm.pack(fill="both", expand=True)
# timer frame(minute + second)
timer_frame = Frame(tab1_firstFm)
timer_frame.pack(side="bottom")
# switch theme
theme = ttk.Checkbutton(tab1_firstFm, text="Dark Mode", style="Switch.TCheckbutton", variable=self.theme_value,
command=lambda: change_theme(self, root))
theme.pack(side=RIGHT, padx=5)
# minute frame
minute_frame = Frame(timer_frame)
minute_frame.grid(row=1, column=1)
# second frame
second_frame = Frame(timer_frame)
second_frame.grid(row=1, column=2)
# minute UI that allow user to input minute
self.minute_ui = ttk.Entry(
minute_frame,
font=("Roboto Mono", 50),
width=5,
justify=RIGHT,
textvariable=self.minutes
)
self.minute_ui.grid(row=0, column=0)
Label(minute_frame, text="m", font=("Roboto Mono", 20)).grid(
row=0, column=1, sticky=S
)
self.minute_ui.bind(
"<Button-1>", lambda e: self.minute_ui.delete(0, tk.END))
self.minute_ui.bind("<Key>", lambda e: limit_digit(self, self.minutes))
# second UI that allow user to input second
self.second_ui = ttk.Entry(
second_frame,
font=("Roboto Mono", 50),
width=5,
justify=RIGHT,
textvariable=self.second,
)
self.second_ui.grid(row=0, column=0)
Label(second_frame, text="s", font=("Roboto Mono", 20)).grid(
row=0, column=1, sticky=S
)
self.second_ui.bind(
"<Button-1>", lambda e: self.second_ui.delete(0, tk.END))
self.second_ui.bind(
"<Key>", lambda e: limit_digit(self, self.second, True))
#set initial value to minute and second
self.minutes.set("00")
self.second.set("00")
# control frame(start + stop)
control_frame = Frame(tab1_secondFm)
control_frame.pack(side="bottom", anchor="w", padx=10, pady=20)
# create Timer_object
self.timer_obj = Timer(self, root)
self.start_button = ttk.Button(
control_frame, text="Start", width=15, compound=LEFT,
command=lambda: self.timer_obj.start(),
padding=(1, 1)
)
self.start_button.grid(row=0, column=0)
self.stop_button = ttk.Button(
control_frame, width=15, compound=LEFT,
command=lambda: (
self.timer_obj.stop()) if not self.stop_flag else reset_timer(self, root),
padding=(1, 1)
)
self.stop_button.grid(row=0, column=1)
self.notebook.bind('<<NotebookTabChanged>>',
lambda e: change_theme(self, root))
#Stopwatch Tab_UI
self.canvas = Canvas(self.tab2, width=900, height=350)
self.canvas.pack(expand=True)
#draw a circle path and circle
self.path = self.canvas.create_oval(
296, 329, 604, 21, outline="grey", width=5)
self.circle = self.canvas.create_oval(
434.6, 344.4, 465.4, 313.6, fill='light grey', outline='light grey')
# Extract the center and radius of the path
self.center_x, self.center_y, self.radius = calculate_path_details(
self)
# Set the initial angle for the circle
self.angle = 0
# Define the speed of the circle
self.speed = 0.035
# add control
self.sp_control = Frame(self.tab2)
self.sp_control.pack(pady=5)
self.stop_watchBtn = ttk.Button(self.sp_control,
text='',
compound='center',
width=30,
command=lambda: start_stopwatch(self, root) if not self.sp_start_flag else stop_stopwatch(self, root))
self.stop_watchBtn.grid(row=0, column=0)
self.reset_button = ttk.Button(
self.sp_control,
compound='center',
width=15,
)
# stop watch label
stop_watch_frame = Frame(self.canvas)
stop_watch_frame.place(relx=0.36, rely=0.4)
self.minute_label = Label(stop_watch_frame, font=(
"Roboto Mono", 35), textvariable=self.sw_minute)
self.minute_label.grid(row=0, column=0)
self.second_label = Label(stop_watch_frame, font=(
"Roboto Mono", 35), textvariable=self.sw_second)
self.second_label.grid(row=0, column=2)
self.centisecond = Label(stop_watch_frame, font=(
"Roboto Mono", 35), textvariable=self.sw_centi)
self.centisecond.grid(row=0, column=4, sticky='s')
# insert (:)
Label(stop_watch_frame, text=":", font=(
"Roboto Mono", 35)).grid(row=0, column=1)
Label(stop_watch_frame, text=":", font=(
"Roboto Mono", 35)).grid(row=0, column=3, sticky='w')
#set the initial value
self.sw_centi.set("00")
self.sw_second.set("00")
self.sw_minute.set("00")
#lap frame:
self.lap_frame = Frame(self.canvas)
self.lap_frame.place(x=50,y=40)
#lap name
self.lap_name1 =self.lap_name =Label(self.lap_frame,text="Lap 1 :")
self.lap_name2 =Label(self.lap_frame,text="Lap 2 :")
self.lap_name3 =Label(self.lap_frame,text="Lap 3 :")
self.lap_name4 =Label(self.lap_frame,text="Lap 4 :")
self.lap_name5 =Label(self.lap_frame,text="Lap 5 :")
#lap duration
self.lap1=Label(self.lap_frame,textvariable=self.lap_1)
self.lap2=Label(self.lap_frame,textvariable=self.lap_2)
self.lap3=Label(self.lap_frame,textvariable=self.lap_3)
self.lap4=Label(self.lap_frame,textvariable=self.lap_4)
self.lap5=Label(self.lap_frame,textvariable=self.lap_5)
self.lap_spt1=Label(self.lap_frame,
text="────────────",
font=("Roboto Mono", 12))
self.lap_spt2=Label(self.lap_frame,
text="────────────",
font=("Roboto Mono", 12))
self.lap_spt3=Label(self.lap_frame,
text="────────────",
font=("Roboto Mono", 12))
self.lap_spt4=Label(self.lap_frame,
text="────────────",
font=("Roboto Mono", 12))
self.lap_spt5=Label(self.lap_frame,
text="────────────",
font=("Roboto Mono", 12))
def close_win(self, root):
stop_stopwatch(self, root)
self.timer_obj.stop()
root.after(300, root.destroy())
def main():
# Reference: https://stackoverflow.com/questions/4066027/making-tkinter-windows-show-up-in-the-taskbar
window = Tk()
minimize_helper = Toplevel(window) # add a taskbar icon
minimize_helper.iconbitmap("Assets\\my_logo.ico")
minimize_helper.title("Multi Timer")
#remove title bar for custom title bar
window.overrideredirect(True)
minimize_helper.attributes("-alpha", 0.0)
# move window without title bar
window.bind('<Button-1>', lambda e: origin_cords(e, window))
window.bind('<B1-Motion>', lambda e: move(e, window))
#add ui
ui = UI(window, minimize_helper)
#apply change to buttons when theme change
change_theme(ui, window)
# toplevel follows root taskbar events (minimize, restore)
def onRootIconify(event): window.withdraw()
minimize_helper.bind("<Unmap>", onRootIconify)
def onRootDeiconify(event): window.deiconify()
minimize_helper.bind("<Map>", onRootDeiconify)
window_ = Frame(master=window)
window_.mainloop()
if __name__ == "__main__":
main()