Improve console suppression for windowed mode on Windows
Refactored console window suppression logic to better handle PyInstaller windowed applications and prevent console flicker. Added creation flags and redirected subprocess streams in multiple modules to ensure hidden console windows when running subprocesses. Introduced a utility function for running subprocesses with hidden console and updated all relevant subprocess calls to use it.
This commit is contained in:
@@ -1,14 +1,50 @@
|
||||
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()
|
||||
# Early detection and handling of windowed mode to prevent console window flicker
|
||||
def setup_windowed_mode():
|
||||
"""
|
||||
Setup windowed mode handling to prevent console window flickering.
|
||||
This must be called as early as possible in the application startup.
|
||||
"""
|
||||
if getattr(sys, 'frozen', False):
|
||||
# Running as compiled executable (PyInstaller)
|
||||
if sys.stdout is None or sys.stderr is None:
|
||||
# Windowed mode - completely suppress console output
|
||||
import io
|
||||
null_stream = io.StringIO()
|
||||
if sys.stdout is None:
|
||||
sys.stdout = null_stream
|
||||
if sys.stderr is None:
|
||||
sys.stderr = null_stream
|
||||
else:
|
||||
# Console mode but compiled - redirect to null to prevent window flicker
|
||||
if os.name == 'nt': # Windows
|
||||
try:
|
||||
import io
|
||||
# Create a null output stream
|
||||
null_stream = io.StringIO()
|
||||
# Only redirect if we detect this might cause console window issues
|
||||
# Check if we're not already in a console
|
||||
try:
|
||||
# Try to get console window handle - if this fails, we're likely windowed
|
||||
import ctypes
|
||||
kernel32 = ctypes.windll.kernel32
|
||||
console_window = kernel32.GetConsoleWindow()
|
||||
if console_window == 0:
|
||||
# No console window exists, redirect to prevent one from appearing
|
||||
sys.stdout = null_stream
|
||||
sys.stderr = null_stream
|
||||
except:
|
||||
# If console detection fails, err on the side of caution
|
||||
sys.stdout = null_stream
|
||||
sys.stderr = null_stream
|
||||
except:
|
||||
# If all else fails, continue normally
|
||||
pass
|
||||
|
||||
# Call windowed mode setup immediately
|
||||
setup_windowed_mode()
|
||||
|
||||
from PySide6.QtWidgets import QApplication, QMessageBox
|
||||
|
||||
|
||||
Reference in New Issue
Block a user