import tkinter as tk import logging import sys import os # --- Setup Python Path for MVC Imports --- # Get the absolute path of the directory containing main.py current_dir = os.path.dirname(os.path.abspath(__file__)) # Add the current directory to sys.path to allow importing from model/, view/, controller/ sys.path.insert(0, current_dir) # Import the main Model class and the message queue from app_logger from model.key_press_model import KeyPressModel # Updated import path from model.app_logger import message_queue, setup_logging_from_config # Updated import for logging setup from view.view import KeyPressView from controller.controller import KeyPressController # --- Global Logging Setup will now be handled by setup_logging_from_config from model.app_logger --- # No direct logging setup here, just calling the setup function. logger = logging.getLogger(__name__) # Logger for the main.py module itself if __name__ == "__main__": # Set up logging early using the config file provided by app_logger current_log_file_path = setup_logging_from_config() # --- Python Environment Diagnostics (Included for completeness) --- print(f"--- Python Environment Diagnostics ---") print(f"Python Executable: {sys.executable}") print(f"Python Version: {sys.version}") print(f"Python Path (sys.path):") for p in sys.path: print(f" {p}") print(f"--- End Diagnostics ---") logger.info("Main: Application starting up.") logger.info(f"Main: Logging all output to file: {current_log_file_path}") main_error_occurred = False try: root = tk.Tk() # Instantiate Model, View, and Controller model = KeyPressModel() controller = KeyPressController(model, None) view = KeyPressView(root, controller) controller.view = view controller.initial_setup_after_view_is_ready() root.protocol("WM_DELETE_WINDOW", controller.on_app_close) logger.info("Main: GUI initialized. Starting main loop.") root.mainloop() except Exception as e: logger.critical(f"Main: An unhandled exception occurred in the main application loop: {e}", exc_info=True) main_error_occurred = True finally: logger.info("Main: Application loop ended. Ensuring all background processes are stopped.") print("Application closed. Jarvis signing off.") if main_error_occurred and not getattr(sys, 'frozen', False): print("\nAn unexpected error occurred. Press Enter to exit the console...") input()