-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
86 lines (71 loc) · 3.68 KB
/
gui.py
File metadata and controls
86 lines (71 loc) · 3.68 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
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
from compression import compress_data, decompress_data
from network import send_data, receive_data
from visualization import plot_compression_stats
class NetworkCompressionToolGUI:
def __init__(self):
self.root = tk.Tk()
self.root.title("Network-Based Data Compression Tool")
self.root.geometry("400x400") # Set a larger size for the window
self.file_path = ""
self.algorithm = "zlib"
self.create_widgets()
def create_widgets(self):
file_frame = ttk.LabelFrame(self.root, text="File Operations", padding=(10, 10))
file_frame.pack(padx=10, pady=10, fill="both", expand=True)
ttk.Button(file_frame, text="Select File", command=self.select_file).grid(row=0, column=0, pady=10)
# Compression Algorithm Dropdown
self.algorithm_var = tk.StringVar(value="zlib")
ttk.Label(file_frame, text="Select Compression Algorithm:").grid(row=1, column=0, pady=5)
ttk.OptionMenu(file_frame, self.algorithm_var, "zlib", "zlib", "gzip", "bz2").grid(row=2, column=0, pady=5)
# Frame for Send/Receive
action_frame = ttk.LabelFrame(self.root, text="Data Transmission", padding=(10, 10))
action_frame.pack(padx=10, pady=10, fill="both", expand=True)
# Send Data Button
ttk.Button(action_frame, text="Send Data", command=self.send_data).grid(row=0, column=0, padx=10, pady=10)
# Receive Data Button
ttk.Button(action_frame, text="Receive Data", command=self.receive_data).grid(row=0, column=1, padx=10, pady=10)
# Visualization Button
ttk.Button(action_frame, text="Visualize Compression", command=self.visualize).grid(row=1, column=0, columnspan=2, pady=10)
def select_file(self):
self.file_path = filedialog.askopenfilename(title="Select a file")
if self.file_path:
messagebox.showinfo("File Selected", f"File: {self.file_path}")
def send_data(self):
if not self.file_path:
messagebox.showerror("Error", "Please select a file first")
return
try:
with open(self.file_path, 'rb') as f:
original_data = f.read()
compressed_data = compress_data(original_data, self.algorithm_var.get())
send_data("127.0.0.1", 65432, compressed_data) # Ensure the receiver is running on this IP/Port
messagebox.showinfo("Success", "Data sent successfully")
except Exception as e:
messagebox.showerror("Error", f"Failed to send data: {str(e)}")
def receive_data(self):
try:
received_data = receive_data("127.0.0.1", 65432) # Ensure receiver is running
decompressed_data = decompress_data(received_data, self.algorithm_var.get())
with open("received_file.txt", 'wb') as f:
f.write(decompressed_data)
messagebox.showinfo("Success", "Data received and decompressed successfully")
except Exception as e:
messagebox.showerror("Error", f"Failed to receive data: {str(e)}")
def visualize(self):
if not self.file_path:
messagebox.showerror("Error", "Please select a file first")
return
try:
plot_compression_stats(self.file_path, self.algorithm_var.get())
except Exception as e:
messagebox.showerror("Error", f"Failed to visualize: {str(e)}")
def run(self):
self.root.mainloop()
# To start the GUI
def run_gui():
gui_app = NetworkCompressionToolGUI()
gui_app.run()
# Uncomment the line below to run the GUI
# run_gui()