-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
98 lines (78 loc) · 3.23 KB
/
app.py
File metadata and controls
98 lines (78 loc) · 3.23 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
import sys
import time
import pygetwindow as gw
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem, QVBoxLayout, QWidget
from PyQt5.QtCore import QTimer, Qt
import threading
from PyQt5.QtWidgets import QHeaderView
# Initialize variables
current_window = None
start_time = None
app_data = {}
data_lock = threading.Lock()
class HistoryApp(QMainWindow):
def __init__(self):
super().__init__()
# Create a timer to update the active window
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_active_window)
self.timer.start(1000) # Check every 1 second
self.setWindowTitle("History Viewer")
self.setGeometry(100, 100, 800, 600)
# Create a table to display history
self.history_table = QTableWidget(self)
self.history_table.setColumnCount(3)
self.history_table.setHorizontalHeaderLabels(["Time", "Activity", "Time Spent"])
self.history_table.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch)
# Create a layout for the table
layout = QVBoxLayout()
layout.addWidget(self.history_table)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
def update_active_window(self):
global current_window, start_time
new_window = gw.getActiveWindow()
if new_window != current_window:
# Calculate the time spent on the previous window
if current_window is not None:
end_time = time.time()
elapsed_time = end_time - start_time
app_name = current_window.title
with data_lock:
if app_name in app_data:
app_data[app_name] += elapsed_time
else:
app_data[app_name] = elapsed_time
current_datetime = time.strftime("%Y-%m-%d %H:%M:%S")
with open("TrackRecords.txt", "a") as file:
file.write(f"{current_datetime} - Time spent on {app_name}: {elapsed_time:.2f} seconds\n")
current_window = new_window
start_time = time.time()
self.update_table()
def update_table(self):
self.history_table.setRowCount(len(app_data))
row = 0
for app, time_spent in app_data.items():
current_datetime = time.strftime("%I:%M %p")
self.history_table.setItem(row, 0, QTableWidgetItem(current_datetime))
self.history_table.setItem(row, 1, QTableWidgetItem(f"{app}"))
self.history_table.setItem(row, 2, QTableWidgetItem(f"{time_spent:.2f} seconds"))
row += 1
def closeEvent(self, event):
self.timer.stop()
event.accept()
if __name__ == "__main__":
current_window = gw.getActiveWindow()
start_time = time.time()
app = QApplication(sys.argv)
window = HistoryApp()
window.show()
def track_app_time():
while True:
window.update_active_window()
time.sleep(1) # Check the active window every 1 second
tracking_thread = threading.Thread(target=track_app_time)
tracking_thread.daemon = True
tracking_thread.start()
sys.exit(app.exec_())