Improvements on the UI and some other parts

This commit is contained in:
Deniz Şafak
2024-03-12 22:18:37 +03:00
committed by GitHub
parent 3051663c22
commit 95e660bfb2
+79 -21
View File
@@ -24,10 +24,10 @@ def center_window(window, width, height):
y = (screen_height - height) // 2 y = (screen_height - height) // 2
window.geometry(f"{width}x{height}+{x}+{y}") window.geometry(f"{width}x{height}+{x}+{y}")
def select_profile(): def run_profile():
global selected_profile global selected_profile
refresh_profile_details()
selected_index = listbox.curselection() selected_index = listbox.curselection()
if selected_index: if selected_index:
selected_index = int(selected_index[0]) selected_index = int(selected_index[0])
selected_profile = profiles[selected_index] selected_profile = profiles[selected_index]
@@ -35,6 +35,7 @@ def select_profile():
# Define a function to update the profile details # Define a function to update the profile details
def update_profile_details(event): def update_profile_details(event):
global selected_profile
selected_index = listbox.curselection() selected_index = listbox.curselection()
if selected_index: if selected_index:
selected_index = int(selected_index[0]) selected_index = int(selected_index[0])
@@ -42,24 +43,41 @@ def update_profile_details(event):
profile_name.config(text=f"Selected Profile: {selected_profile['name']}") profile_name.config(text=f"Selected Profile: {selected_profile['name']}")
profile_details.config(state=tk.NORMAL) profile_details.config(state=tk.NORMAL)
profile_details.delete(1.0, tk.END) profile_details.delete(1.0, tk.END)
# Insert profile details with all variables except "name"WWWW # Insert profile details with all variables except "name"WWWW
profile_details.tag_configure("bold", font=("Courier New", 10, "bold"), foreground="#08872a") profile_details.tag_configure("bold", font=("Courier New", 10, "bold"), foreground="#08872a")
for key, value in selected_profile.items(): for key, value in selected_profile.items():
if key != 'name': # Skip displaying the "name" variable if key != 'name': # Skip displaying the "name" variable
profile_details.insert(tk.END, f"{key}: ", "bold") profile_details.insert(tk.END, f"{key}: ", "bold")
profile_details.insert(tk.END, f"{value}\n") profile_details.insert(tk.END, f"{value}\n")
profile_details.config(state=tk.DISABLED) profile_details.config(state=tk.DISABLED)
else: else:
reset_profile_details()
def reset_profile_details():
profile_name.config(text="Selected Profile: ") profile_name.config(text="Selected Profile: ")
profile_details.config(state=tk.NORMAL) profile_details.config(state=tk.NORMAL)
profile_details.delete(1.0, tk.END) profile_details.delete(1.0, tk.END)
profile_details.config(state=tk.DISABLED) profile_details.config(state=tk.DISABLED)
# Function to refresh the profile details
def refresh_profile_details():
global profiles
# Get the index of the currently selected profile
selected_index = listbox.curselection()
selected_index = int(selected_index[0]) if selected_index else None
# Reload configuration from file
with open("config.json", "r") as f:
config = json.load(f)
profiles = config.get("profiles", [])
listbox.delete(0, tk.END) # Clear the listbox
for profile in profiles:
listbox.insert(tk.END, profile['name'])
# Reselect the previously selected profile if it exists
if selected_index is not None and selected_index < len(profiles):
listbox.select_set(selected_index)
update_profile_details(None)
# Function to open the config file
def open_config():
os.system("config.json")
# Load configuration from file # Load configuration from file
with open("config.json", "r") as f: with open("config.json", "r") as f:
config = json.load(f) config = json.load(f)
@@ -96,16 +114,21 @@ scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
listbox.config(yscrollcommand=scrollbar.set) listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview) scrollbar.config(command=listbox.yview)
# Bind the listbox selection event to update the profile details # Bind the listbox selection event to update and refresh the profile details
listbox.bind('<<ListboxSelect>>', update_profile_details) listbox.bind('<<ListboxSelect>>', lambda event: (update_profile_details(event), refresh_profile_details()))
listbox.bind('<Return>', run_profile) # Run the selected profile when the Enter key is pressed
# Create a frame for displaying profile details # Create a frame for displaying profile details
details_frame = tk.Frame(root) details_frame = tk.Frame(root)
details_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True, padx=10, pady=10) details_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True, padx=10, pady=10)
# Create a label for the selected profile name # Create a frame for the profile name and the Refresh button
profile_name = tk.Label(details_frame, text="Selected Profile: ", anchor="w") profile_name_frame = tk.Frame(details_frame)
profile_name.pack(fill="x", pady=(0, 5)) profile_name_frame.pack(fill="x", pady=(0, 5))
# Create the label for the selected profile name
profile_name = tk.Label(profile_name_frame, text="Selected Profile: ", anchor="w")
profile_name.pack(side=tk.LEFT)
# Create a Text widget for displaying profile details with a fixed height and scrollbars # Create a Text widget for displaying profile details with a fixed height and scrollbars
profile_details = Text(details_frame, wrap=tk.WORD, height=10, state=tk.DISABLED, exportselection=False) profile_details = Text(details_frame, wrap=tk.WORD, height=10, state=tk.DISABLED, exportselection=False)
@@ -125,11 +148,43 @@ def on_closing():
root.protocol("WM_DELETE_WINDOW", on_closing) root.protocol("WM_DELETE_WINDOW", on_closing)
# Create the "Select" button # Create the "Refresh" button within the profile name frame
select_button = tk.Button( refresh_button = tk.Button(
profile_name_frame,
text="Refresh",
command=refresh_profile_details,
padx=5,
pady=0,
fg="white",
bg="grey",
activebackground="#606060",
activeforeground="white",
relief=tk.RAISED,
borderwidth=2,
)
refresh_button.pack(side=tk.RIGHT)
# Create the "Config" button
config_button = tk.Button(
details_frame,
text="Config",
command=open_config,
padx=30,
pady=10,
fg="white",
bg="grey",
activebackground="#606060",
activeforeground="white",
relief=tk.RAISED,
borderwidth=2,
)
config_button.pack(side=tk.LEFT, fill="x", padx=(0, 2), pady=(10, 0), expand=False)
# Create the "Run" button
run_button = tk.Button(
details_frame, details_frame,
text="Run", text="Run",
command=select_profile, command=run_profile,
padx=10, padx=10,
pady=10, pady=10,
fg="white", fg="white",
@@ -139,7 +194,10 @@ select_button = tk.Button(
relief=tk.RAISED, relief=tk.RAISED,
borderwidth=2, borderwidth=2,
) )
select_button.pack(fill="x", pady=(10, 0)) run_button.pack(side=tk.LEFT, fill="x", padx=(2, 0), pady=(10, 0), expand=True)
# Configure weights for horizontal stretching
details_frame.pack_propagate(False)
# Start with no profile selected # Start with no profile selected
selected_profile = None selected_profile = None
@@ -168,7 +226,6 @@ if selected_profile:
expected_ssid = ssid expected_ssid = ssid
expected_ssid_lower = None expected_ssid_lower = None
# Function to send the request # Function to send the request
def send_request(): def send_request():
session = requests.Session() session = requests.Session()
@@ -222,7 +279,7 @@ def exit_application(icon, item):
os._exit(0) os._exit(0)
# Queue to store log messages # Queue to store log messages
log_messages = deque(maxlen=20) log_messages = deque(maxlen=30)
log_dialog = None log_dialog = None
log_text = None log_text = None
log_dialog_open = False log_dialog_open = False
@@ -239,17 +296,18 @@ def update_window_geometry(event):
x, y = event.x, event.y x, y = event.x, event.y
width, height = event.width, event.height width, height = event.width, event.height
# Function to show the log dialog # Function to show the log dialog with the currently selected profile name
def show_log_dialog(): def show_log_dialog():
global log_dialog, log_text, log_dialog_open global log_dialog, log_text, log_dialog_open
if not log_dialog_open: if not log_dialog_open:
log_dialog_open = True log_dialog_open = True
if log_dialog: if log_dialog:
log_dialog.title(f"Log Messages ({selected_profile['name']})") # Update title with profile name
log_dialog.deiconify() log_dialog.deiconify()
update_log() update_log()
else: else:
log_dialog = tk.Tk() log_dialog = tk.Tk()
log_dialog.title("Log Messages") log_dialog.title(f"Log Messages ({selected_profile['name']})") # Update title with profile name
log_text = tk.Text(log_dialog) log_text = tk.Text(log_dialog)
log_frame = tk.Frame(log_dialog) log_frame = tk.Frame(log_dialog)
log_frame.pack(fill=tk.BOTH, expand=True) log_frame.pack(fill=tk.BOTH, expand=True)
@@ -342,7 +400,7 @@ def check_network_status():
response = send_request() response = send_request()
if response.ok: if response.ok:
errorcount = 0 errorcount = 0
sleepcount = 10 sleepcount = 20
message = "Request was successful. Checking the internet connection in {} seconds...".format(str(sleepcount)) message = "Request was successful. Checking the internet connection in {} seconds...".format(str(sleepcount))
add_to_log(message) add_to_log(message)
save_to_file(message) save_to_file(message)