-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkinter_app.py
94 lines (73 loc) · 3.04 KB
/
tkinter_app.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
import datetime
import os
import sqlite3
import tkinter as tk
from tkinter import messagebox
from staff.Collector import Collector
class App:
def __init__(self, root):
self.root = root
self.root.title("Stakan file statistics")
self.directory = "C:/stakan"
self.a = Collector(self.directory)
self.db_path = self.a.get_sqlite_path()
self.menu_bar = tk.Menu(self.root)
self.root.config(menu=self.menu_bar)
self.menu_bar.add_command(
label="Статистика по расширениям", command=self.show_extensions_statistics
)
self.menu_bar.add_command(
label="Статистика ТОП-10 по размеру",
command=self.show_top_10_size_statistics,
)
total_files_label = tk.Label(root, text=f"Всего файлов: {self.count_files()}")
total_files_label.pack()
last_indexed_label = tk.Label(
root, text=f"Дата последней индексации: {self.last_indexed_time()}"
)
last_indexed_label.pack()
def show_extensions_statistics(self):
if self.a.updated_db():
messagebox.showinfo("Update Database", "Необходимо обновить базу данных.")
return
connection = sqlite3.connect(self.db_path)
query = """
SELECT SUBSTR(file_name, INSTR(file_name, '.') + 1) AS extension, COUNT(*)
FROM metadata
GROUP BY extension
ORDER BY COUNT(*) DESC
"""
result = connection.execute(query).fetchall()
connection.close()
self.show_statistics(result, "Статистика по расширениям")
def show_top_10_size_statistics(self):
if self.a.updated_db():
messagebox.showinfo("Update Database", "Необходимо обновить базу данных.")
return
connection = sqlite3.connect(self.db_path)
query = "SELECT * FROM metadata ORDER BY file_size DESC LIMIT 10"
result = connection.execute(query).fetchall()
connection.close()
self.show_statistics(result, "Статистика ТОП-10 по размеру")
def show_statistics(self, data, title):
statistics_window = tk.Toplevel(self.root)
statistics_window.title(title)
listbox = tk.Listbox(statistics_window, width=40, height=15)
listbox.pack()
for item in data:
listbox.insert(tk.END, f"{item[0]}: {item[1]}")
def count_files(self):
connection = sqlite3.connect(self.db_path)
query = "SELECT COUNT(*) FROM metadata"
result = connection.execute(query).fetchone()[0]
connection.close()
return result
def last_indexed_time(self):
last_indexed_time = datetime.datetime.fromtimestamp(
os.path.getmtime(self.db_path)
).strftime("%Y-%m-%d %H:%M:%S")
return last_indexed_time
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()