# -*- coding: utf-8 -*- """HeadlessUI.root 改成惰性属性。 原来 root 是普通字段(初始 None),AgentUI 删掉之后, Actions.show_image 之类依赖 ui.root 的地方在静默模式下就拿不到父窗口。 改成属性:访问 ui.root 时按需创建(隐藏的 Tk 根窗口),永远不显示。 """ from __future__ import annotations import sys from pathlib import Path HEADLESS = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote\windows" r"\lanremote\headless.py") OLD = ''' self.root = None # 惰性创建 self._tk_ready = False''' NEW = ''' self._root = None # 惰性创建,见下面的 root 属性 self._tk_ready = False''' PROP_ANCHOR = ''' def _ensure_root(self):''' PROP_NEW = ''' @property def root(self): """Tk 根窗口,按需创建且**始终隐藏**。 AgentUI 已经删掉了,但 Actions 里投图/全屏大字这些还要一个父窗口。 这里惰性建一个 withdrew 的根窗口 —— 它本身永远不显示, 所以启动时屏幕上依然什么都没有。 """ return self._ensure_root() @root.setter def root(self, value): self._root = value def _ensure_root(self):''' FIXES = [ (OLD, NEW, "root 字段改名"), (PROP_ANCHOR, PROP_NEW, "root 属性"), ] # _ensure_root 内部要用 _root INNER_OLD = ''' if self.root is not None: return self.root''' INNER_NEW = ''' if self._root is not None: return self._root''' SET_OLD = ''' self.root = root self._start_pump() return root''' SET_NEW = ''' self._root = root self._start_pump() return root''' PUMP_OLD = ''' if self.root is not None: try: self.root.update() except Exception: # noqa: BLE001 return continue''' PUMP_NEW = ''' if self._root is not None: try: self._root.update() except Exception: # noqa: BLE001 return continue''' PUMP2_OLD = ''' if self.root is not None: try: self.root.update() except Exception: # noqa: BLE001 return''' PUMP2_NEW = ''' if self._root is not None: try: self._root.update() except Exception: # noqa: BLE001 return''' UI_CALL_OLD = ''' if self.root is None:''' UI_CALL_NEW = ''' if self._root is None:''' STOP_OLD = ''' if self.root is not None: try: self.root.quit() self.root.destroy() except Exception: # noqa: BLE001 pass self.root = None''' STOP_NEW = ''' if self._root is not None: try: self._root.quit() self._root.destroy() except Exception: # noqa: BLE001 pass self._root = None''' ALL = FIXES + [ (INNER_OLD, INNER_NEW, "_ensure_root 内引用"), (SET_OLD, SET_NEW, "_ensure_root 赋值"), (PUMP_OLD, PUMP_NEW, "pump 引用 1"), (PUMP2_OLD, PUMP2_NEW, "pump 引用 2"), (UI_CALL_OLD, UI_CALL_NEW, "ui_call 判断"), (STOP_OLD, STOP_NEW, "stop 引用"), ] def main() -> int: text = HEADLESS.read_text(encoding="utf-8") applied, skipped = 0, [] for old, new, tag in ALL: if old not in text: skipped.append(tag) print(f" [跳过] {tag} —— 锚点没找到") continue text = text.replace(old, new, 1) applied += 1 print(f" {tag}: OK") HEADLESS.write_text(text, encoding="utf-8") print(f"\n 应用 {applied} / {len(ALL)}") if skipped: print(f" 跳过: {', '.join(skipped)}") import ast try: ast.parse(HEADLESS.read_text(encoding="utf-8")) print(" headless.py: 语法 OK") except SyntaxError as exc: print(f" headless.py: 语法错误 {exc}") return 1 return 0 if __name__ == "__main__": sys.exit(main())