import tkinter as tk from tkinter import messagebox import sys import traceback # Import necessary components from gui.svg_processor_gui import SVGProcessorApp from config_manager import ConfigManager def run_application(): """ Initializes and runs the SVGProcessorApp. Handles Tkinter setup, window centering, and global exception handling. """ root = None # Initialize root to None try: # Create ConfigManager first config_manager = ConfigManager() root = tk.Tk() # Pass the config manager to the app app = SVGProcessorApp(root, config_manager=config_manager) # Add confirmation dialog on close def confirm_exit(): # Ensure app has the on_closing method before calling if hasattr(app, 'on_closing') and callable(app.on_closing): if messagebox.askokcancel("Exit", "Save configuration and exit?"): app.on_closing() # This should handle saving and root.destroy() else: # Fallback if on_closing doesn't exist or app failed init if messagebox.askokcancel("Exit", "Exit Application?"): if root and root.winfo_exists(): root.destroy() root.protocol("WM_DELETE_WINDOW", confirm_exit) # Center the window window_width = 1200 window_height = 800 screen_width = root.winfo_screenwidth() screen_height = root.winfo_screenheight() # Calculate coordinates ensuring they are integers x_coordinate = int((screen_width / 2) - (window_width / 2)) y_coordinate = int((screen_height / 2) - (window_height / 2)) # Prevent negative coordinates if screen is smaller than window x_coordinate = max(0, x_coordinate) y_coordinate = max(0, y_coordinate) root.geometry(f"{window_width}x{window_height}+{x_coordinate}+{y_coordinate}") root.mainloop() except Exception as e: # Global exception handler print("--- APPLICATION CRITICAL ERROR ---", file=sys.stderr) traceback.print_exc() # Print detailed traceback to console/stderr error_message = f"An unexpected critical error occurred:\n{str(e)}" try: # Try showing messagebox even if Tk might be unstable messagebox.showerror("Application Error", error_message) except Exception as msg_e: # Fallback to printing if messagebox fails print(f"Failed to show error messagebox: {msg_e}", file=sys.stderr) print(f"APPLICATION CRITICAL ERROR: {error_message}", file=sys.stderr) return 1 # Indicate error exit code return 0 # Indicate successful exit (or normal closure) if __name__ == "__main__": exit_code = run_application() sys.exit(exit_code)