Repository navigation
Expand file tree
/
Copy pathgui.py
More file actions
184 lines (168 loc) · 7.41 KB
/
Copy pathgui.py
File metadata and controls
184 lines (168 loc) · 7.41 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
import tkinter as tk
from tkinter import ttk
from config import REFRESH_INTERVAL_SEC
from logic import TrafficMonitorLogic
class TrafficMonitorApp:
COLORS = {
"background": "#10151c",
"panel": "#18212b",
"panel_light": "#202c38",
"text": "#edf4f7",
"muted": "#8fa1ad",
"cyan": "#55d6c2",
"blue": "#70a7ff",
"line": "#2b3946",
"danger": "#ff7d8b",
}
def __init__(self, root):
self.root = root
self.monitor = TrafficMonitorLogic()
self.refresh_job = None
self.root.title("Local Traffic Monitor")
self.root.geometry("1020x680")
self.root.minsize(760, 520)
self.root.configure(bg=self.COLORS["background"])
self._configure_styles()
self._build_layout()
self.root.protocol("WM_DELETE_WINDOW", self.close)
self.refresh()
def _configure_styles(self):
style = ttk.Style(self.root)
style.theme_use("clam")
style.configure(
"Traffic.Treeview",
background=self.COLORS["panel"],
fieldbackground=self.COLORS["panel"],
foreground=self.COLORS["text"],
rowheight=34,
borderwidth=0,
font=("TkDefaultFont", 10),
)
style.configure(
"Traffic.Treeview.Heading",
background=self.COLORS["panel_light"],
foreground=self.COLORS["muted"],
borderwidth=0,
font=("TkDefaultFont", 9, "bold"),
padding=(10, 9),
)
style.map(
"Traffic.Treeview",
background=[("selected", "#2d5260")],
foreground=[("selected", self.COLORS["text"])],
)
def _build_layout(self):
root = self.root
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(2, weight=1)
header = tk.Frame(root, bg=self.COLORS["background"])
header.grid(row=0, column=0, sticky="ew", padx=34, pady=(28, 18))
header.grid_columnconfigure(1, weight=1)
tk.Label(
header,
text="LOCAL TRAFFIC MONITOR",
bg=self.COLORS["background"],
fg=self.COLORS["text"],
font=("TkDefaultFont", 20, "bold"),
).grid(row=0, column=0, sticky="w")
self.status_label = tk.Label(
header,
text="● LIVE",
bg=self.COLORS["background"],
fg=self.COLORS["cyan"],
font=("TkDefaultFont", 10, "bold"),
)
self.status_label.grid(row=0, column=1, sticky="e")
cards = tk.Frame(root, bg=self.COLORS["background"])
cards.grid(row=1, column=0, sticky="ew", padx=34, pady=(0, 24))
for column in range(4):
cards.grid_columnconfigure(column, weight=1, uniform="card")
self.cards = {}
card_data = (
("DOWNLOAD", "recv_speed", self.COLORS["blue"]),
("UPLOAD", "sent_speed", self.COLORS["cyan"]),
("TOTAL RECEIVED", "total_recv", self.COLORS["text"]),
("TOTAL SENT", "total_sent", self.COLORS["text"]),
)
for column, (title, key, accent) in enumerate(card_data):
card = tk.Frame(cards, bg=self.COLORS["panel"], padx=18, pady=16)
card.grid(row=0, column=column, sticky="nsew", padx=(0 if column == 0 else 6, 0 if column == 3 else 6))
tk.Label(
card, text=title, bg=self.COLORS["panel"], fg=self.COLORS["muted"],
font=("TkDefaultFont", 9, "bold"),
).pack(anchor="w")
value = tk.Label(
card, text="--", bg=self.COLORS["panel"], fg=accent,
font=("TkDefaultFont", 17, "bold"),
)
value.pack(anchor="w", pady=(7, 0))
self.cards[key] = value
content = tk.Frame(root, bg=self.COLORS["background"])
content.grid(row=2, column=0, sticky="nsew", padx=34, pady=(0, 26))
content.grid_columnconfigure(0, weight=1)
content.grid_rowconfigure(1, weight=1)
tk.Label(
content, text="ACTIVE CONNECTIONS", bg=self.COLORS["background"],
fg=self.COLORS["muted"], font=("TkDefaultFont", 10, "bold"),
).grid(row=0, column=0, sticky="w", pady=(0, 10))
table_frame = tk.Frame(content, bg=self.COLORS["panel"])
table_frame.grid(row=1, column=0, sticky="nsew")
table_frame.grid_columnconfigure(0, weight=1)
table_frame.grid_rowconfigure(0, weight=1)
columns = ("process", "pid", "status", "target", "location")
self.connection_table = ttk.Treeview(
table_frame, columns=columns, show="headings", style="Traffic.Treeview",
)
headings = {
"process": "PROCESS", "pid": "PID", "status": "STATUS",
"target": "REMOTE TARGET", "location": "LOCATION",
}
widths = {"process": 190, "pid": 75, "status": 135, "target": 260, "location": 170}
for column in columns:
self.connection_table.heading(column, text=headings[column])
self.connection_table.column(column, width=widths[column], anchor="w", stretch=column == "target")
self.connection_table.grid(row=0, column=0, sticky="nsew")
scrollbar = ttk.Scrollbar(table_frame, orient="vertical", command=self.connection_table.yview)
scrollbar.grid(row=0, column=1, sticky="ns")
self.connection_table.configure(yscrollcommand=scrollbar.set)
footer = tk.Frame(root, bg=self.COLORS["background"])
footer.grid(row=3, column=0, sticky="ew", padx=34, pady=(0, 18))
footer.grid_columnconfigure(0, weight=1)
self.connection_count = tk.Label(
footer, text="0 connections", bg=self.COLORS["background"], fg=self.COLORS["muted"],
font=("TkDefaultFont", 9),
)
self.connection_count.grid(row=0, column=0, sticky="w")
tk.Label(
footer, text=f"Refresh every {REFRESH_INTERVAL_SEC}s", bg=self.COLORS["background"],
fg=self.COLORS["muted"], font=("TkDefaultFont", 9),
).grid(row=0, column=1, sticky="e")
def refresh(self):
try:
bandwidth = self.monitor.get_system_bandwidth()
connections = self.monitor.get_active_connections()
for key, value in bandwidth.items():
self.cards[key].configure(text=value + ("/s" if key.endswith("speed") else ""))
for item in self.connection_table.get_children():
self.connection_table.delete(item)
for connection in connections:
self.connection_table.insert(
"", "end",
values=(
connection["process"], connection["pid"], connection["status"],
connection["target"], connection["location"],
),
)
self.connection_count.configure(text=f"{len(connections)} connection{'s' if len(connections) != 1 else ''}")
self.status_label.configure(text="● LIVE", fg=self.COLORS["cyan"])
except Exception as error:
self.status_label.configure(text=f"● ERROR: {error}", fg=self.COLORS["danger"])
self.refresh_job = self.root.after(REFRESH_INTERVAL_SEC * 1000, self.refresh)
def close(self):
if self.refresh_job is not None:
self.root.after_cancel(self.refresh_job)
self.root.destroy()
def run_gui():
root = tk.Tk()
TrafficMonitorApp(root)
root.mainloop()