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.
This commit is contained in:
@@ -1,4 +1,14 @@
|
|||||||
import sys
|
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
|
from PySide6.QtWidgets import QApplication, QMessageBox
|
||||||
|
|
||||||
|
|||||||
+20
-18
@@ -94,9 +94,12 @@ def setup_logging():
|
|||||||
|
|
||||||
# Console handler - INFO and above, with colors
|
# Console handler - INFO and above, with colors
|
||||||
# Check if stdout is available (it might be None in PyInstaller windowed apps)
|
# 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
|
stdout_available = sys.stdout is not None
|
||||||
|
is_windowed_app = getattr(sys, 'frozen', False) and sys.stdout is None
|
||||||
|
|
||||||
if stdout_available:
|
# Skip console output if we're in a windowed application to prevent brief console window
|
||||||
|
if stdout_available and not is_windowed_app:
|
||||||
try:
|
try:
|
||||||
logger.add(
|
logger.add(
|
||||||
sys.stdout,
|
sys.stdout,
|
||||||
@@ -117,13 +120,13 @@ def setup_logging():
|
|||||||
except Exception:
|
except Exception:
|
||||||
stdout_available = False
|
stdout_available = False
|
||||||
|
|
||||||
# If stdout is not available, try stderr or skip console logging entirely
|
# If stdout is not available or we're in windowed mode, try stderr for critical messages only
|
||||||
if not stdout_available:
|
if (not stdout_available or is_windowed_app) and not is_windowed_app:
|
||||||
try:
|
try:
|
||||||
if sys.stderr is not None:
|
if sys.stderr is not None:
|
||||||
logger.add(
|
logger.add(
|
||||||
sys.stderr,
|
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}",
|
format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {message}",
|
||||||
catch=True,
|
catch=True,
|
||||||
)
|
)
|
||||||
@@ -159,28 +162,27 @@ def setup_logging():
|
|||||||
# If file logging fails, just log to console
|
# If file logging fails, just log to console
|
||||||
logger.warning(f"Could not set up file logging: {e}")
|
logger.warning(f"Could not set up file logging: {e}")
|
||||||
|
|
||||||
# Log startup message if we have any handlers
|
# Log startup message
|
||||||
if logger._core.handlers:
|
try:
|
||||||
logger.info("YTSage logging system initialized")
|
logger.info("YTSage logging system initialized")
|
||||||
if log_dir and log_dir.exists():
|
if log_dir and log_dir.exists():
|
||||||
logger.debug(f"Log directory: {log_dir}")
|
logger.debug(f"Log directory: {log_dir}")
|
||||||
else:
|
else:
|
||||||
logger.warning("File logging disabled - could not create log directory")
|
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 no handlers were successfully added, add a null handler to prevent errors
|
||||||
if not logger._core.handlers:
|
try:
|
||||||
# Add a minimal handler that just discards messages
|
# Try to add a minimal fallback handler if needed
|
||||||
# This prevents loguru from complaining about no handlers
|
|
||||||
import tempfile
|
import tempfile
|
||||||
|
temp_log = Path(tempfile.gettempdir()) / "ytsage_temp.log"
|
||||||
try:
|
logger.add(temp_log, level="ERROR", catch=True)
|
||||||
# Try to add a temporary file handler as last resort
|
except Exception:
|
||||||
temp_log = Path(tempfile.gettempdir()) / "ytsage_temp.log"
|
# If even that fails, we're in a very restricted environment
|
||||||
logger.add(temp_log, level="ERROR", catch=True)
|
# loguru should handle this gracefully with its internal fallbacks
|
||||||
except Exception:
|
pass
|
||||||
# If even that fails, we're in a very restricted environment
|
|
||||||
# loguru should handle this gracefully with its internal fallbacks
|
|
||||||
pass
|
|
||||||
|
|
||||||
return logger
|
return logger
|
||||||
|
|
||||||
|
|||||||
@@ -4,8 +4,12 @@ import subprocess
|
|||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
import time
|
import time
|
||||||
|
import warnings
|
||||||
from pathlib import Path
|
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 pkg_resources
|
||||||
import requests
|
import requests
|
||||||
from packaging import version
|
from packaging import version
|
||||||
|
|||||||
Reference in New Issue
Block a user