-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
127 lines (113 loc) · 5.74 KB
/
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
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
import tkinter as tk
from tkinter import *
from directoryReader import DirectoryReader
from tkinter import filedialog
import sys
import threading
from thread_manager import Ready
class View:
def __init__(self, master, width, height):
self.width = width
self.height = height
self.master = master
self.bar_height = 100
self.display_size_index = -1
master.title("MemoryAnalyzer")
self.master.resizable(False, False)
self.frame = tk.Frame(self.master, width=width, height=height)
self.frame.pack(expand=True)
self.colors = ["red", "green", "blue", "yellow", "black", "orange"]
self.canvas = tk.Canvas(self.frame, width=800, height=500, scrollregion=(0, 0, 0, 500), bg="#1f4068")
self.canvas.bind("<Button 1>", self.click_canvas)
self.canvas.bind("<Motion>", self.hover_canvas)
self.canvas.pack()
self.master.bind_all("<MouseWheel>", self._on_mousewheel)
self.scrollbar = Scrollbar(self.frame, orient=VERTICAL)
self.scrollbar.pack(side=BOTTOM, fill=Y, expand=True)
self.scrollbar.config(command=self.canvas.yview)
self.canvas.config(yscrollcommand=self.scrollbar.set)
self.canvas.pack(side=LEFT, expand=True, fill=BOTH)
self.rectangles = []
self.labels = []
self.menu = tk.Menu(self.master)
self.menu.add_command(label="Pick Folder", command=self.pickFile)
self.menu.add_command(label="Update", command=self.update_dirs)
self.master.config(menu=self.menu)
def pickFile(self):
f = filedialog.askdirectory()
if f == "":
sys.exit(0)
self.directory_reader = DirectoryReader()
self.directory_reader.path = f.replace("/", "\\")
self.start()
def create_percent_bars(self, percents):
self.canvas.delete("all")
self.canvas.create_text(15, 10, text="Back", tag="back", fill="#dddddd")
self.canvas.create_text(700, 10, text="Size: ", tag="size", fill="#dddddd")
for index, per in enumerate(percents[self.directory_reader.level]):
x1, y1, x2, y2 = self.get_percent_bar_cords(per, index)
dir_path = self.directory_reader.sub_dirs[self.directory_reader.level][index]
dir_name = dir_path.split("\\")[-1]
if len(dir_name) > 12:
while len(dir_name) > 12:
dir_name = dir_name[:-1]
dir_name += "..."
label = self.canvas.create_text(((x1 + x2) / 2, y2+10), text=dir_name, tag=index, fill="#dddddd")
self.rectangles.append(self.canvas.create_rectangle(x1, y1, x2, y2, fill="#e43f5a", tag=index, outline="#dddddd"))
def get_percent_bar_cords(self, percent, index):
indices_in_row = 7.01
index += 1
percent = 1 - percent
offset_y = int(index / indices_in_row)
x1, y1, x2, y2 = 35, 50, 100, 200
return x1 + (x2 * index) - (x2 * (indices_in_row) * offset_y), y1 + ((y2 - y1) * percent) + (y2 * offset_y), x2 * index - (x2 * (indices_in_row) * offset_y), y2 + (y2 * offset_y)
def show_dirs(self):
self.create_percent_bars(self.directory_reader.sizes_percent)
self.display_size_index = -1
rest_elements = len(self.rectangles) % 14
scroll_size_y = int((len(self.rectangles) / 14)) * 400
if rest_elements > 0 and rest_elements < 8:
scroll_size_y += 250
elif rest_elements > 0:
scroll_size_y += 500
#print(scroll_size_y)
self.canvas.config(scrollregion=(0, 0, 0, scroll_size_y))
def start(self):
thread = threading.Thread(name="thread1", target=self.directory_reader.read_dir)
thread.start()
ready = Ready(self.directory_reader, self, thread)
thread_ready = threading.Thread(target=ready.check_read_state)
thread_ready.start()
def _on_mousewheel(self, event):
self.canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
def update_dirs(self):
path = self.directory_reader.path
self.directory_reader = DirectoryReader()
self.directory_reader.path = path
self.start()
def click_canvas(self, event):
if self.canvas.find_withtag(CURRENT):
tags = self.canvas.gettags(CURRENT)
if tags[0] == "back":
if self.directory_reader.level != 0:
thread_next = threading.Thread(name="thread_next", target=lambda: self.directory_reader.last_path())
ready = Ready(self.directory_reader, self, thread_next)
thread_ready = threading.Thread(target=ready.check_read_state)
thread_next.start()
thread_ready.start()
else:
index = int(tags[0])
if self.directory_reader.sub_dirs[self.directory_reader.level][index] != "Files":
thread_next = threading.Thread(name="thread_next", target=lambda: self.directory_reader.next_path(index))
ready = Ready(self.directory_reader, self, thread_next)
thread_ready = threading.Thread(target=ready.start_after_thread)
thread_next.start()
thread_ready.start()
def hover_canvas(self, event):
if self.canvas.find_withtag(CURRENT):
tags = self.canvas.gettags(CURRENT)
if tags[0] != "back" and tags[0] != "size":
index = int(tags[0])
if self.display_size_index != index:
self.canvas.itemconfigure("size", text="Size: " + str(self.directory_reader.sub_dirs_sizes[self.directory_reader.level][index]) + "MB, " + str(self.directory_reader.sizes_percent[self.directory_reader.level][index]) + "%")
self.display_size_index = index