-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.py
More file actions
254 lines (208 loc) · 10.5 KB
/
Copy pathstats.py
File metadata and controls
254 lines (208 loc) · 10.5 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
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
import tkinter as tk
import json
import time
import os
from tkinter import font
import sys
from tkinter import ttk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
def get_resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
class HoverImageButton(tk.Button):
def __init__(self, master=None, image=None, command=None, **kwargs):
tk.Button.__init__(self, master=master, image=image, command=command, **kwargs)
self.default_image = image
self.hover_image = image
self.bind("<Enter>", self.on_enter)
self.bind("<Leave>", self.on_leave)
def on_enter(self, event=None):
if self.hover_image:
self.config(image=self.hover_image)
self.configure(cursor="hand2")
def on_leave(self, event=None):
if self.default_image:
self.config(image=self.default_image)
self.configure(cursor="")
class MainApp:
def __init__(self, master):
self.master = master
self.master.title("PRO | Spoti")
self.width = 1000
self.height = 563
self.master.iconbitmap(icon_path)
screen_width = self.master.winfo_screenwidth()
screen_height = self.master.winfo_screenheight()
x = (screen_width - self.width) // 2
y = (screen_height - self.height) // 2
self.master.geometry(f"{self.width}x{self.height}+{x}+{y}")
bag_image_path = os.path.join(script_dir, 'bag.png')
self.bg_image = tk.PhotoImage(file=bag_image_path)
self.canvas = tk.Canvas(self.master, width=self.width, height=self.height)
self.canvas.pack()
self.canvas.create_image(0, 0, anchor="nw", image=self.bg_image)
start_image_path = os.path.join(script_dir, 'start.png')
stop_image_path = os.path.join(script_dir, 'stop.png')
self.start_image = tk.PhotoImage(file=start_image_path).subsample(2)
self.stop_image = tk.PhotoImage(file=stop_image_path).subsample(2)
self.start_button = tk.Button(self.master, image=self.start_image, command=self.start_timer, bd=0, highlightthickness=0, bg='#232323', activebackground='#232323', highlightbackground=self.canvas.cget('bg'))
self.stop_button = tk.Button(self.master, image=self.stop_image, command=self.stop_timer, bd=0, highlightthickness=0, state=tk.DISABLED, bg='#232323', activebackground='#232323', highlightbackground=self.canvas.cget('bg'))
self.start_button_window = self.canvas.create_window(200, 400, anchor="nw", window=self.start_button)
self.stop_button_window = self.canvas.create_window(650, 400, anchor="nw", window=self.stop_button)
cross_image_path = os.path.join(script_dir, 'cross.png')
self.save_close_image = tk.PhotoImage(file=cross_image_path).subsample(2)
self.save_close_button = tk.Button(self.master, image=self.save_close_image, command=self.save_and_close, bd=0, highlightthickness=0, bg='#232323', activebackground='#232323', highlightbackground=self.canvas.cget('bg'))
self.save_close_button_window = self.canvas.create_window(935, 13, anchor="nw", window=self.save_close_button)
self.graph_window_open = False
graph_image_path = os.path.join(script_dir, 'graph.png')
self.graph_button_image = tk.PhotoImage(file=graph_image_path).subsample(2)
self.graph_button = tk.Button(self.master, image=self.graph_button_image, command=self.toggle_graph_window, bd=0,
highlightthickness=0, bg='#232323', activebackground='#232323',
highlightbackground=self.canvas.cget('bg'))
self.graph_button_window = self.canvas.create_window(420, 480, anchor="nw", window=self.graph_button)
self.custom_font = font.Font(family="Bodoni MT Black", size=40)
self.timer_label = tk.Label(self.master, text="00:00:00", font=self.custom_font, bg='#232323', fg='white', activebackground='#232323', highlightbackground=self.canvas.cget('bg'), padx=0, pady=-5000)
self.timer_label_window = self.canvas.create_window(360, 75, anchor="nw", window=self.timer_label)
self.master.overrideredirect(True)
self.canvas.bind("<ButtonPress-1>", self.start_move)
self.canvas.bind("<ButtonRelease-1>", self.stop_move)
self.canvas.bind("<B1-Motion>", self.on_motion)
self.timer_started = False
appdata_path = os.getenv('APPDATA')
self.pro_studies_path = os.path.join(appdata_path, 'Pro-Studies')
if not os.path.exists(self.pro_studies_path):
os.makedirs(self.pro_studies_path)
self.timer_data_path = os.path.join(self.pro_studies_path, 'timer_data.json')
self.load_or_create_timer_data()
def start_move(self, event):
self.x = event.x
self.y = event.y
def stop_move(self, event):
self.x = None
self.y = None
def on_motion(self, event):
deltax = event.x - self.x
deltay = event.y - self.y
x = self.master.winfo_x() + deltax
y = self.master.winfo_y() + deltay
self.master.geometry(f"+{x}+{y}")
def get_initial_time(self):
today_date = time.strftime('%Y-%m-%d')
elapsed_time = self.timer_data.get(today_date, 0)
return self.format_time(elapsed_time)
def start_timer(self):
self.start_time = time.time() - self.get_elapsed_time()
self.timer_started = True
self.update_timer()
self.start_button.config(state=tk.DISABLED)
self.stop_button.config(state=tk.NORMAL)
def stop_timer(self):
if hasattr(self, 'start_time'):
elapsed_time = time.time() - self.start_time
self.save_elapsed_time(elapsed_time)
self.timer_label.config(text=f"{self.format_time(elapsed_time)}")
self.timer_started = False
self.start_button.config(state=tk.NORMAL)
self.stop_button.config(state=tk.DISABLED)
def load_or_create_timer_data(self):
try:
with open(self.timer_data_path, 'r') as file:
self.timer_data = json.load(file)
self.start_time = time.time() - self.timer_data.get(time.strftime('%Y-%m-%d'), 0)
elapsed_time = self.timer_data.get(time.strftime('%Y-%m-%d'), 0)
self.timer_label.config(text=f"{self.format_time(elapsed_time)}")
except FileNotFoundError:
self.timer_data = {}
self.save_timer_data()
def load_timer(self):
try:
with open('timer_data.json', 'r') as file:
data = json.load(file)
if time.strftime('%Y-%m-%d') in data:
self.start_time = time.time() - data[time.strftime('%Y-%m-%d')]
elapsed_time = data[time.strftime('%Y-%m-%d')]
self.timer_label.config(text=f"{self.format_time(elapsed_time)}")
else:
self.start_time = time.time()
except FileNotFoundError:
self.start_time = time.time()
def save_timer_data(self):
with open(self.timer_data_path, 'w') as file:
json.dump(self.timer_data, file)
def update_timer(self):
if self.timer_started:
elapsed_time = time.time() - self.start_time
self.timer_label.config(text=f"{self.format_time(elapsed_time)}")
self.master.after(1000, self.update_timer)
def save_elapsed_time(self, elapsed_time):
self.timer_data[time.strftime('%Y-%m-%d')] = elapsed_time
self.save_timer_data()
def save_and_close(self):
if self.timer_started:
self.stop_timer()
if hasattr(self, 'graph_window') and self.graph_window.winfo_exists():
self.graph_window.destroy()
self.master.destroy()
self.master.quit()
def toggle_graph_window(self):
if self.graph_window_open:
self.close_graph_window()
else:
self.show_weekly_graph()
def close_graph_window(self):
if self.graph_window_open:
self.graph_window.destroy()
self.graph_window_open = False
def show_weekly_graph(self):
weekly_data = self.get_weekly_data()
self.graph_window = tk.Toplevel(self.master)
self.graph_window.title("Pro | Study Weekly Graph")
self.graph_window.geometry(f"{self.width}x{self.height}")
bagg_image_path = os.path.join(script_dir, 'bag.png')
graph_bg_image = tk.PhotoImage(file=bagg_image_path)
graph_canvas = tk.Canvas(self.graph_window, width=self.width, height=self.height)
graph_canvas.pack()
graph_canvas.create_image(0, 0, anchor="nw", image=graph_bg_image)
self.graph_window.overrideredirect(True)
fig, ax = plt.subplots(figsize=(8, 6))
ax.plot(weekly_data.keys(), weekly_data.values(), marker='o')
ax.set_xlabel('Day of the Week')
ax.set_ylabel('Hours')
ax.set_title('Weekly Study Time')
ax.grid(True)
canvas = FigureCanvasTkAgg(fig, master=graph_canvas)
canvas.draw()
canvas.get_tk_widget().pack()
self.graph_window_open = True
def get_weekly_data(self):
weekly_data = {
'Monday': 0,
'Tuesday': 0,
'Wednesday': 0,
'Thursday': 0,
'Friday': 0,
'Saturday': 0,
'Sunday': 0
}
for date, elapsed_time in self.timer_data.items():
day_of_week = time.strptime(date, '%Y-%m-%d').tm_wday
day_name = time.strftime('%A', time.strptime(date, '%Y-%m-%d'))
weekly_data[day_name] += elapsed_time / 3600
return weekly_data
def format_time(self, elapsed_time):
hours, remainder = divmod(elapsed_time, 3600)
minutes, seconds = divmod(remainder, 60)
return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}"
def get_elapsed_time(self):
return self.timer_data.get(time.strftime('%Y-%m-%d'), 0)
if __name__ == "__main__":
script_dir = getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__)))
icon_path = os.path.join(script_dir, 'pro.ico')
root = tk.Tk()
app = MainApp(root)
root.deiconify()
root.mainloop()