AutoPercenty3/main.py

78 lines
2.4 KiB
Python

import ctypes
from PySide6.QtWidgets import QApplication
from gui import AutoPercentyGUI
from logger_module import setup_logger
import asyncio
import os
# 절전모드를 방지하는 설정 값
ES_CONTINUOUS = 0x80000000
ES_SYSTEM_REQUIRED = 0x00000001
def prevent_sleep():
"""절전모드 방지를 위해 시스템 설정을 변경"""
ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED)
def allow_sleep():
"""절전모드 방지 설정을 해제"""
ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS)
async def process_qt_events(app, stop_event):
"""PySide6의 이벤트를 처리하는 비동기 함수"""
try:
while not stop_event.done():
app.processEvents()
await asyncio.sleep(0.01) # 10ms마다 Qt 이벤트 처리
except asyncio.CancelledError:
# 취소 시 안전하게 종료
pass
async def main():
# 로깅 설정
logger = setup_logger('default_logger', f'appTranslator.log')
# 절전모드 방지 활성화
prevent_sleep()
app = None
window = None # window 변수를 None으로 초기화하여 finally 블록에서 참조 가능하도록 함
stop_event = asyncio.Future() # 종료 이벤트 생성
try:
app = QApplication([])
try:
os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
os.environ["QT_SCALE_FACTOR"] = "1"
ctypes.windll.shcore.SetProcessDpiAwareness(1)
except Exception as e:
logger.error(f"DPI 인식 설정 실패: {e}")
window = AutoPercentyGUI(logger, app)
window.show()
# QApplication.exec_()을 사용하여 Qt 이벤트 루프 시작
await asyncio.gather(
process_qt_events(app, stop_event),
window.run_async_tasks(),
return_exceptions=True
)
# 이 부분은 exec_()를 호출했기 때문에 도달하지 않습니다.
# app.exec_()
except Exception as e:
logger.exception(f"Main function error: {e}")
finally:
allow_sleep()
if not stop_event.done():
stop_event.set_result(True)
if window:
await window.close() # await 추가
if app:
app.quit() #QApplication을 명시적으로 종료
if __name__ == '__main__':
asyncio.run(main()) # 비동기 함수는 asyncio.run()으로 실행