- main.py: 增加show_banner()启动说明、各阶段[INFO]日志、结果摘要、任意键退出 - file_selector.py: 重写为路径输入→验证→空输入弹窗回退→不存在循环重试 - run.bat: 新建启动脚本(chcp 65001, mode con cols=80 lines=20, color 0B, title固定署名, pause) - Code/docs/modification-assessment.md: 修改需求评估文档
50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
"""File selector — GUI dialog or CLI fallback.
|
|
|
|
Provides a single function ``select_file`` that:
|
|
1. Opens a tkinter file-dialog when a display is available.
|
|
2. Falls back to ``sys.argv[1]`` in headless environments.
|
|
"""
|
|
|
|
import sys
|
|
from typing import Optional
|
|
|
|
|
|
def select_file() -> Optional[str]:
|
|
"""Open a file-selection dialog and return the chosen path, or None.
|
|
|
|
Returns
|
|
-------
|
|
str | None
|
|
Selected file path, or ``None`` if the user cancelled / no
|
|
fallback is available.
|
|
"""
|
|
# Try tkinter GUI dialog first
|
|
try:
|
|
import tkinter
|
|
import tkinter.filedialog
|
|
|
|
root = tkinter.Tk()
|
|
root.withdraw() # hide the main window
|
|
root.attributes("-topmost", True)
|
|
|
|
filepath = tkinter.filedialog.askopenfilename(
|
|
title="选择 PinMAP 文件",
|
|
filetypes=[
|
|
("Excel 文件", "*.xls *.xlsx"),
|
|
("所有文件", "*.*"),
|
|
],
|
|
)
|
|
root.destroy()
|
|
|
|
if filepath:
|
|
# tkinter may return a Tcl object; normalise to str
|
|
return str(filepath)
|
|
return None
|
|
|
|
except (ImportError, Exception):
|
|
# No display / no tkinter — fall back to CLI argument
|
|
if len(sys.argv) > 1:
|
|
return sys.argv[1]
|
|
print("[WARN] 无 GUI 环境且未提供命令行参数")
|
|
return None
|