Fix this code: import subprocess as sp import customtkinter import os from PIL import…
- User
Fix this code: import subprocess as sp import customtkinter import os from PIL import Image from tkinter import messagebox import logging import time
Erstelle ein Protokoll
logging.basicConfig(filename='shutdown.log', level=logging.INFO)
class App(customtkinter.CTk): def init(self): super().init()
font_large_bold = customtkinter.CTkFont(size=30, weight="bold") self.title("Shutdown Assistant") self.geometry("700x450") # Set grid layout 1x2 self.grid_rowconfigure(0, weight=1) self.grid_columnconfigure(1, weight=1) # Load images with light and dark mode image image_path = r"C:\Users\hardy\Documents\School\Shutdown Assistant\test_images" self.logo_image = customtkinter.CTkImage(Image.open(os.path.join(image_path, "CustomTkinter_logo_single.png")), size=(26, 26)) self.large_test_image = customtkinter.CTkImage(Image.open(os.path.join(image_path, "large_test_image.png")), size=(450, 130)) self.image_icon_image = customtkinter.CTkImage(Image.open(os.path.join(image_path, "image_icon_light.png")), size=(20, 20)) self.home_image = customtkinter.CTkImage(light_image=Image.open(os.path.join(image_path, "home_dark.png")), dark_image=Image.open(os.path.join(image_path, "home_light.png")), size=(20, 20)) self.remote_image = customtkinter.CTkImage(light_image=Image.open(os.path.join(image_path, "remote_light.png")), dark_image=Image.open(os.path.join(image_path, "remote_dark.png")), size=(9, 20)) self.big_remote_image = customtkinter.CTkImage(Image.open(os.path.join(image_path, "big_remote.png")), size=(450, 130)) # Create navigation frame self.navigation_frame = customtkinter.CTkFrame(self, corner_radius=0) self.navigation_frame.grid(row=0, column=0, sticky="nsew") self.navigation_frame.grid_rowconfigure(4, weight=1) self.navigation_frame_label = customtkinter.CTkLabel(self.navigation_frame, text=" Shutdown Assistant", image=self.logo_image, compound="left", font=customtkinter.CTkFont(size=15, weight="bold")) self.navigation_frame_label.grid(row=0, column=0, padx=20, pady=20) button_height = 40 # Define a consistent button height self.home_button = customtkinter.CTkButton(self.navigation_frame, corner_radius=0, height=button_height, border_spacing=10, text="Home", fg_color="transparent", text_color=("gray10", "gray90"), hover_color=("gray70", "gray30"), image=self.home_image, anchor="w", command=self.home_button_event) self.home_button.grid(row=1, column=0, sticky="ew") self.remote_button = customtkinter.CTkButton(self.navigation_frame, corner_radius=0, height=button_height, border_spacing=10, text="Remote Shutdown", fg_color="transparent", text_color=("gray10", "gray90"), hover_color=("gray70", "gray30"), image=self.remote_image, anchor="w", command=self.remote_button_event) self.remote_button.grid(row=2, column=0, sticky="ew") self.exit_button = customtkinter.CTkButton(self.navigation_frame, text="Exit", command=self.exit_application) self.exit_button.grid(row=5, column=0, padx=20, pady=10, sticky="s") self.appearance_mode_menu = customtkinter.CTkOptionMenu(self.navigation_frame, values=["Light", "Dark", "System"], command=self.change_appearance_mode_event) self.appearance_mode_menu.grid(row=6, column=0, padx=20, pady=20, sticky="s") # Create home frame with shutdown functionality self.home_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent") self.home_frame.grid_columnconfigure(0, weight=1) self.home_frame_large_image_label = customtkinter.CTkLabel(self.home_frame, text="", image=self.large_test_image) self.home_frame_large_image_label.grid(row=0, column=0, padx=20, pady=10) self.time_entry_label = customtkinter.CTkLabel(self.home_frame, text="Shutdown Delay (1h, 30m, 45s):") self.time_entry_label.grid(row=1, column=0, padx=20, pady=5) self.time_entry = customtkinter.CTkEntry(self.home_frame) self.time_entry.grid(row=2, column=0, padx=20, pady=5) self.shutdown_button = customtkinter.CTkButton(self.home_frame, text="Shutdown", command=self.shutdown, height=60, font=font_large_bold) self.shutdown_button.grid(row=3, column=0, padx=20, pady=10, sticky="ew") self.cancel_button = customtkinter.CTkButton(self.home_frame, text="Cancel Shutdown", command=self.cancel_shutdown, fg_color="gray40") self.cancel_button.grid(row=4, column=0, padx=20, pady=10) # Create remote shutdown frame with IP entry self.remote_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent") self.remote_frame.grid_columnconfigure(0, weight=1) self.remote_frame_large_image_label = customtkinter.CTkLabel(self.remote_frame, text="", image=self.big_remote_image) self.remote_frame_large_image_label.grid(row=0, column=0, padx=20, pady=10) self.ip_entry_label = customtkinter.CTkLabel(self.remote_frame, text="IP-Adresse für Remote:") self.ip_entry = customtkinter.CTkEntry(self.remote_frame) self.ip_entry_label.grid(row=1, column=0, padx=20, pady=5) self.ip_entry.grid(row=2, column=0, padx=20, pady=5) self.time_entry_label_remote = customtkinter.CTkLabel(self.remote_frame, text="Shutdown Delay (1h, 30m, 45s):") self.time_entry_label_remote.grid(row=3, column=0, padx=20, pady=5) self.time_entry_remote = customtkinter.CTkEntry(self.remote_frame) self.time_entry_remote.grid(row=4, column=0, padx=20, pady=5) self.shutdown_button_remote = customtkinter.CTkButton(self.remote_frame, text="Remote Shutdown", command=self.remote_shutdown, height=60, font=font_large_bold) self.shutdown_button_remote.grid(row=5, column=0, padx=20, pady=10, sticky="ew") self.cancel_button_remote = customtkinter.CTkButton(self.remote_frame, text="Cancel Shutdown", command=self.cancel_shutdown, fg_color="gray40") self.cancel_button_remote.grid(row=6, column=0, padx=20, pady=10) # Select default frame self.select_frame_by_name("home") self.countdown_window = None self.countdown_timer_label = None self.countdown_timer_dropdown = None self.countdown_timer_dropdown_value = None self.open_countdown_button = customtkinter.CTkButton(self.navigation_frame, text="Open Countdown", command=self.open_countdown) self.open_countdown_button.grid(row=7, column=0, padx=20, pady=10, sticky="s") def open_countdown(self): if self.countdown_window is not None: self.countdown_window.destroy() self.countdown_window = customtkinter.CTkToplevel(self, width=200, height=100) self.countdown_window.title("Countdown") self.countdown_timer_label = customtkinter.CTkLabel(self.countdown_window, text="00:00:00", font=("Helvetica", 24)) self.countdown_timer_label.pack(pady=20) self.countdown_timer_dropdown = customtkinter.CTkOptionMenu(self.countdown_window, values=["Local", "Remote", "IP 1", "IP 2"], command=self.update_countdown_timer) self.countdown_timer_dropdown.pack(pady=10) self.countdown_window.after(1000, self.update_countdown_timer_label) def update_countdown_timer_label(self): if self.countdown_timer_dropdown_value == "Local": time_input = self.time_entry.get() elif self.countdown_timer_dropdown_value == "Remote": time_input = self.time_entry_remote.get() else: time_input = "00:00:00" # default value for IP timers if time_input.strip() == "": # check if the time input is empty countdown_time = "00:00:00" else: shutdown_time = self.parse_time_input(time_input) remaining_time = shutdown_time - int(time.time()) if remaining_time < 0: remaining_time = 0 self.countdown_timer_label.configure(text="00:00:00") # stop updating the label when timer reaches 00:00:00 else: hours, remainder = divmod(remaining_time, 3600) minutes, seconds = divmod(remainder, 60) countdown_time = f"{hours:02d}:{minutes:02d}:{seconds:02d}" self.countdown_timer_label.configure(text=countdown_time) if remaining_time > 0: # only update the label if the timer is still running self.countdown_window.after(1000, self.update_countdown_timer_label) def update_countdown_timer(self, value): self.countdown_timer_dropdown_value = value self.update_countdown_timer_label() def select_frame_by_name(self, name): """Selects the frame by name and adjusts button color.""" self.home_button.configure(fg_color=("gray75", "gray25") if name == "home" else "transparent") self.remote_button.configure(fg_color=("gray75", "gray25") if name == "remote" else "transparent") if name == "home": self.home_frame.grid(row=0, column=1, sticky="nsew") self.remote_frame.grid_forget() elif name == "remote": self.remote_frame.grid(row=0, column=1, sticky="nsew") self.home_frame.grid_forget() def home_button_event(self): """Handles the home button click event.""" self.select_frame_by_name("home") def remote_button_event(self): """Handles the remote button click event.""" self.select_frame_by_name("remote") def change_appearance_mode_event(self, new_appearance_mode): """Handles changes in the appearance mode.""" customtkinter.set_appearance_mode(new_appearance_mode) def parse_time_input(self, time_str): time_in_seconds = 0 time_str = time_str.strip().lower() if ':' in time_str: hours, minutes, seconds = map(int, time_str.split(':')) time_in_seconds = hours * 3600 + minutes * 60 + seconds else: parts = time_str.split() for part in parts: if part.endswith('h'): time_in_seconds += int(part[:-1]) * 3600 elif part.endswith('m'): time_in_seconds += int(part[:-1]) * 60 elif part.endswith('s'): time_in_seconds += int(part[:-1]) else: time_in_seconds += int(part) return time_in_seconds def exit_application(self): """Exits the application.""" self.destroy() exit() def cancel_shutdown(self): """Cancels the shutdown.""" sp.run("shutdown /a", shell=True) def shutdown(self): """Executes the local shutdown command.""" time_input = self.time_entry.get() shutdown_time = self.parse_time_input(time_input) if not self.check_input(shutdown_time): return try: sp.run(f"shutdown /s /t {shutdown_time}", shell=True) logging.info("Shutdown-Befehl gesendet") except Exception as e: self.handle_error(e) def remote_shutdown(self): """Executes the remote shutdown command.""" time_input = self.time_entry_remote.get() shutdown_time = self.parse_time_input(time_input) ip_address = self.ip_entry.get() if not self.check_input(shutdown_time): return if not ip_address: messagebox.showerror("Fehler", "Bitte geben Sie eine gültige IP-Adresse ein.") return try: sp.run(f"shutdown /s /m \\\\{ip_address} /t {shutdown_time}", shell=True) logging.info(f"Remote Shutdown-Befehl an {ip_address} gesendet") except Exception as e: self.handle_error(e) def check_input(self, shutdown_time): """Überprüft die Eingabe.""" if shutdown_time < 0: messagebox.showerror("Fehler", "Bitte geben Sie eine positive Zahl ein.") return False return True def handle_error(self, e): """Behandelt Fehler.""" logging.error(f"Fehler: {e}") messagebox.showerror("Fehler", f"Fehler: {e}") - Assistant