-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
123 lines (88 loc) · 2.74 KB
/
main.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
h## FILE MAIN.PY
import tkinter as tk
import tkinter.font as TkFont
import getpass
import platform
SOURCE = PATH + "clockSource.txt"
running = False
def parse_source(SOURCE):
with open(SOURCE, 'a+') as src:
# This assumes src.readlines() returns an integer
src.seek(0)
localCounter = src.read().strip()
try:
return int(localCounter)
except:
return 0
def write_source(SOURCE, localCounter):
with open(SOURCE, 'a+') as src:
src.seek(0)
src.truncate(0)
src.write(str(localCounter))
def increment():
global localCounter
global imgWrapper
if(running == True):
localCounter+= 1
clockMode = localCounter % 8
img = tk.PhotoImage(file=PATH+"/frame000{0}.png".format(clockMode))
imgWrapper.configure(image=img)
imgWrapper.image = img # Prevent garbage collection from deleting the image
root.after(1000, increment)
def displayUpdate():
global localCounter, display
display.config(text=formatCounter(localCounter));
root.after(1000, displayUpdate)
# We pass in a localCounter that we can easily manipulate ourselves
def formatCounter(localCounter):
hours = localCounter // 3600
localCounter = localCounter % 3600
minutes = localCounter // 60
localCounter = localCounter % 60
seconds = localCounter
if(seconds < 10):
seconds = "0{0}".format(seconds)
if(minutes < 10):
minutes = "0{0}".format(minutes)
displayString = "{0}:{1}:{2}".format(hours, minutes, seconds)
return displayString
def toggleRunning():
global B
global running
if(running == False):
running = True
B.config(text="Stop", bg = "#ff8c8c", activebackground="#fcb6b6")
return
if(running == True):
running = False
B.config(text="Start", bg = "#8cffad", activebackground="#b6fcca")
return
def on_closing(root):
global localCounter
write_source(SOURCE, localCounter)
def stay_on_top():
global root
root.lift()
root.after(1, stay_on_top)
# MAIN CODE ##
# Create Window
root = tk.Tk() # Set tkinter root
root.geometry('230x48+1000+300')
# Initialize Root Info
root.title('Time Spent on Activity')
root.resizable(0,0)
root.bind("<Destroy>", on_closing)
# Initialize Local Counter
localCounter = parse_source(SOURCE)
# Initialize Clock Image
img = tk.PhotoImage(file=(PATH + "/frame0000.png"))
imgWrapper = tk.Label(root, image=img)
imgWrapper.grid(row=0, column=0)
# Create Display
display = tk.Label(text="0", anchor="w", font=("Less Perfect DOS VGA", 25))
display.grid(row=0, column=1)
B = tk.Button(root, text="Start", command = toggleRunning, bg = "#8cffad", activebackground="#b6fcca")
B.grid(row=0, column=2)
increment()
displayUpdate()
root.mainloop()