From a6a175b3d7f4d5daa6d7a434e4c5265a4045366e Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 27 Aug 2025 22:17:13 +0300 Subject: [PATCH] Improve handling of console window in windowed apps Suppresses console window flicker for PyInstaller windowed applications by redirecting stdout and stderr in main.py. Updates logging setup to avoid console output in windowed mode and adds fallback error logging. Suppresses pkg_resources deprecation warnings to further reduce unwanted console output. --- main.py | 10 ++++++++++ src/core/ytsage_logging.py | 40 ++++++++++++++++++++------------------ src/core/ytsage_utils.py | 4 ++++ 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/main.py b/main.py index cfbb542..a01e374 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,14 @@ import sys +import os + +# Suppress console window for windowed applications (PyInstaller --windowed) +if getattr(sys, 'frozen', False): + # Running as compiled executable + if sys.stdout is None or sys.stderr is None: + # Windowed mode - redirect stdout/stderr to prevent console window flicker + import io + sys.stdout = io.StringIO() + sys.stderr = io.StringIO() from PySide6.QtWidgets import QApplication, QMessageBox diff --git a/src/core/ytsage_logging.py b/src/core/ytsage_logging.py index 7a17589..31b5a68 100644 --- a/src/core/ytsage_logging.py +++ b/src/core/ytsage_logging.py @@ -94,9 +94,12 @@ def setup_logging(): # Console handler - INFO and above, with colors # Check if stdout is available (it might be None in PyInstaller windowed apps) + # Also check if we're running in a windowed mode (likely PyInstaller --windowed) stdout_available = sys.stdout is not None - - if stdout_available: + is_windowed_app = getattr(sys, 'frozen', False) and sys.stdout is None + + # Skip console output if we're in a windowed application to prevent brief console window + if stdout_available and not is_windowed_app: try: logger.add( sys.stdout, @@ -117,13 +120,13 @@ def setup_logging(): except Exception: stdout_available = False - # If stdout is not available, try stderr or skip console logging entirely - if not stdout_available: + # If stdout is not available or we're in windowed mode, try stderr for critical messages only + if (not stdout_available or is_windowed_app) and not is_windowed_app: try: if sys.stderr is not None: logger.add( sys.stderr, - level="WARNING", # Only warnings and errors to stderr + level="ERROR", # Only errors to stderr to minimize console window appearance format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {message}", catch=True, ) @@ -159,28 +162,27 @@ def setup_logging(): # If file logging fails, just log to console logger.warning(f"Could not set up file logging: {e}") - # Log startup message if we have any handlers - if logger._core.handlers: + # Log startup message + try: logger.info("YTSage logging system initialized") if log_dir and log_dir.exists(): logger.debug(f"Log directory: {log_dir}") else: logger.warning("File logging disabled - could not create log directory") + except Exception: + # If logging fails, continue silently + pass # If no handlers were successfully added, add a null handler to prevent errors - if not logger._core.handlers: - # Add a minimal handler that just discards messages - # This prevents loguru from complaining about no handlers + try: + # Try to add a minimal fallback handler if needed import tempfile - - try: - # Try to add a temporary file handler as last resort - temp_log = Path(tempfile.gettempdir()) / "ytsage_temp.log" - logger.add(temp_log, level="ERROR", catch=True) - except Exception: - # If even that fails, we're in a very restricted environment - # loguru should handle this gracefully with its internal fallbacks - pass + temp_log = Path(tempfile.gettempdir()) / "ytsage_temp.log" + logger.add(temp_log, level="ERROR", catch=True) + except Exception: + # If even that fails, we're in a very restricted environment + # loguru should handle this gracefully with its internal fallbacks + pass return logger diff --git a/src/core/ytsage_utils.py b/src/core/ytsage_utils.py index e3a9677..bea694b 100644 --- a/src/core/ytsage_utils.py +++ b/src/core/ytsage_utils.py @@ -4,8 +4,12 @@ import subprocess import sys import tempfile import time +import warnings from pathlib import Path +# Suppress the pkg_resources deprecation warning to prevent console window flicker +warnings.filterwarnings("ignore", message=".*pkg_resources is deprecated.*", category=UserWarning) + import pkg_resources import requests from packaging import version