# -*- coding: utf-8 -*- """1. 修一下说明文字的换行宽度 2. 给首次设置窗口的测试加"卡片必须真的显示出来"的断言 第 2 点很关键: 我之前写的 _card() 返回了内层 frame,而内层挂在从未被 pack 的 外层里 —— 卡片完全不显示(窗口只有 199px 高)。 但控件都"存在",walk 能列出来,原来的测试全过。 只有断言"窗口够高" + "子控件是 mapped 的"才能抓到。 """ from __future__ import annotations import ast import sys from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") UI = ROOT / "windows" / "lanremote" / "ui.py" TEST = ROOT / "_tests" / "test_first_run_setup.py" # ---------------- 1. 换行宽度 ---------------- WRAP_OLD = ''' tk.Label(opt_inner, text="没放行成功的话,配对码不会提交给服务器," "会告诉你怎么处理。", bg=CARD, fg=MUTED, font=FONT_SMALL, justify="left", wraplength=340).pack(anchor="w", pady=(6, 0))''' WRAP_NEW = ''' tk.Label(opt_inner, text="没放行成功的话,配对码不会提交给服务器," "会告诉你怎么处理。", bg=CARD, fg=MUTED, font=FONT_SMALL, justify="left", wraplength=396).pack(anchor="w", pady=(6, 0))''' # ---------------- 2. 测试断言 ---------------- TEST_ANCHOR = ''' check("设置窗口有输入框", found.get("entries", 0) >= 1, str(found))''' TEST_NEW = ''' # ---- 界面必须真的显示出来 ---- # 踩过的坑:_card() 返回了内层 frame,内层挂在从未 pack 的外层里, # 于是两张卡片完全不显示,窗口只有 199px 高 —— 但控件都在, # 光数控件个数查不出来。所以这里断言"够高 + 子控件是 mapped 的"。 metrics = found.get("metrics", {}) print(f" 窗口 {metrics.get('width')}x{metrics.get('height')}," f"mapped 子控件 {metrics.get('mapped')}/{metrics.get('children')}") check("设置窗口够高(卡片真的画出来了)", (metrics.get("height") or 0) >= 400, f"高度只有 {metrics.get('height')}(<400 说明卡片没显示)") check("有多个子控件被真正映射(mapped)", (metrics.get("mapped") or 0) >= 2, metrics) check("配对码输入框是可见的", metrics.get("entry_mapped") is True, metrics) check("设置窗口有输入框", found.get("entries", 0) >= 1, str(found))''' # 在 ready() 里收集 metrics READY_OLD = ''' def ready(win): parts = inspect(win) found["entries"] = len(parts["entries"]) found["checks"] = len(parts["checks"]) found["buttons"] = len(parts["buttons"]) found["labels"] = parts["labels"] found["confirmed"] = fill_and_confirm(win, "789113")''' READY_NEW = ''' def ready(win): root = win.root # 多刷几轮,等布局稳定(刚建好时 winfo_width 还可能是初始值) for _ in range(6): root.update_idletasks() root.update() time.sleep(0.2) parts = inspect(win) found["entries"] = len(parts["entries"]) found["checks"] = len(parts["checks"]) found["buttons"] = len(parts["buttons"]) found["labels"] = parts["labels"] children = root.winfo_children() found["metrics"] = { "width": root.winfo_width(), "height": root.winfo_height(), "children": len(children), "mapped": sum(1 for c in children if c.winfo_ismapped()), "reqheight": root.winfo_reqheight(), "entry_mapped": any(e.winfo_ismapped() for e in parts["entries"]), } found["confirmed"] = fill_and_confirm(win, "789113")''' def main() -> int: ok = True text = UI.read_text(encoding="utf-8") if WRAP_OLD in text: text = text.replace(WRAP_OLD, WRAP_NEW, 1) UI.write_text(text, encoding="utf-8", newline="\n") print(" ui.py:说明文字换行宽度 340 -> 396") else: print(" ui.py:换行宽度锚点没找到(可能已改)") text = TEST.read_text(encoding="utf-8") changed = 0 for old, new in ((READY_OLD, READY_NEW), (TEST_ANCHOR, TEST_NEW)): if old in text: text = text.replace(old, new, 1) changed += 1 else: print(f" [失败] 测试锚点没找到 -> {old.strip()[:60]}") ok = False if changed: TEST.write_text(text, encoding="utf-8", newline="\n") print(f" 测试:加了 {changed} 处界面可见性断言") # 确认测试里有 time 导入 if "import time" not in text: text = text.replace("import tempfile", "import tempfile\nimport time", 1) TEST.write_text(text, encoding="utf-8", newline="\n") print(" 测试:补了 import time") ast.parse(UI.read_text(encoding="utf-8")) ast.parse(TEST.read_text(encoding="utf-8")) print(" 语法都 OK") return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())