-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusage_gui.py
65 lines (49 loc) · 2.21 KB
/
usage_gui.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
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from repeated_timer import Repeated_Timer
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("Repeated timer with Tkinter")
self.geometry("530x200")
self.resizable(0, 0)
self.repeated_timer = None # Declares a repeated_timer variable inside a Tk object.
self.label = tk.Label(self, text=f'Remaining(sec) : {10}', font=('Roboto', 16))
self.label.grid(column=0, row=0, sticky="", padx=(80, 0), pady=(30, 0))
self.start_button = tk.Button(self, text="Start", width=30, overrelief="solid",
font=('Roboto', 16, 'bold'),
command=lambda: self.start_button_clicked())
self.start_button.grid(column=0, row=2, sticky="", padx=(80, 0), pady=(30, 0))
def start_button_clicked(self):
if self.start_button['text'] == 'Start':
'''Start Event'''
self.repeated_timer = Repeated_Timer(interval=1, duration=10, function=timer_tick,
label=self.label) # Assign object to variable repeated_timer
self.repeated_timer.start() # Timer Start
self.start_button['text'] = 'Stop'
self.start_button.configure(fg='red')
else:
'''Stop Event'''
self.repeated_timer.stop() # Timer Stop
self.start_button['text'] = 'Start'
self.start_button.configure(fg='black')
def closing_event(self):
if messagebox.askokcancel("Quit", "Do you want to quit?"):
try:
self.repeated_timer.stop()
except AttributeError:
pass
finally:
self.destroy()
def timer_tick(remaining_time: int, *args: tuple, **kwargs: dict):
# timer tick event!!
# You can put your code in here
# print('timer tick!', args, kwargs)
kwargs['label'].configure(text=f'Remaining(sec) : {remaining_time}')
if remaining_time <= 0:
App.start_button_clicked(app)
if __name__ == '__main__':
app = App()
app.protocol("WM_DELETE_WINDOW", app.closing_event)
app.mainloop()