From cdada11f8180eaa4b024c551570f9d0688413d08 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 3 Jan 2026 20:33:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=AE=9E=E6=97=B6?= =?UTF-8?q?=E6=9F=A5=E7=9C=8B=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E5=B1=95=E7=A4=BA=E6=88=AA=E5=9B=BE=E4=B8=8E?= =?UTF-8?q?=E5=8A=A8=E4=BD=9C=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gui/gui_app.py | 156 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) diff --git a/gui/gui_app.py b/gui/gui_app.py index 657f240..020065a 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -2222,6 +2222,12 @@ def main(): ttk.Checkbutton( realtime_tools, text="自动滚动", variable=auto_scroll_var ).pack(side=LEFT, padx=(0, 8)) + # forward-declare handler to satisfy linters; real function defined later + open_realtime_viewer = lambda: None + + ttk.Button(realtime_tools, text="实时查看", width=10, command=lambda: open_realtime_viewer()).pack( + side=LEFT, padx=(8, 0) + ) log_tab = ttk.Frame(notebook) preview_tab = ttk.Frame(notebook) @@ -2409,6 +2415,156 @@ def main(): except Exception: pass + # --------- 实时查看弹窗(紧凑视图) --------- + def open_realtime_viewer(): + try: + # 如果已打开则直接聚焦 + if hasattr(open_realtime_viewer, "_win") and open_realtime_viewer._win: + try: + open_realtime_viewer._win.lift() + except Exception: + pass + return + + win = tk.Toplevel(root) + win.title("实时查看 - 截图与最简动作流") + win.geometry("520x520") + open_realtime_viewer._win = win + + # 图片与列表 + img_lbl = ttk.Label(win) + img_lbl.pack(fill=X, pady=(6, 4)) + + list_frame = ttk.Frame(win) + list_frame.pack(fill=BOTH, expand=True, padx=6, pady=6) + listbox = tk.Listbox(list_frame, height=12) + listbox.pack(side=LEFT, fill=BOTH, expand=True) + scrollbar = ttk.Scrollbar(list_frame, orient=tk.VERTICAL, command=listbox.yview) + scrollbar.pack(side=RIGHT, fill=Y) + listbox.configure(yscrollcommand=scrollbar.set) + + stop_flag = {"stop": False} + + def close_win(): + stop_flag["stop"] = True + try: + win.destroy() + except Exception: + pass + open_realtime_viewer._win = None + + win.protocol("WM_DELETE_WINDOW", close_win) + + # use module-level `re` + + def tail_read(path: str, max_bytes: int = 64 * 1024) -> list[str]: + try: + if not os.path.exists(path): + return [] + with open(path, "rb") as f: + f.seek(0, os.SEEK_END) + sz = f.tell() + f.seek(max(0, sz - max_bytes)) + return f.read().decode(errors="ignore").splitlines() + except Exception: + return [] + + def parse_minimal(line: str) -> tuple[str, str] | None: + if "耗时" not in line: + return None + m = re.search(r"耗时[:=]?\s*([0-9]+(?:\.[0-9]+)?)\s*s", line) + dur = None + if m: + dur = m.group(1) + "s" + else: + m2 = re.search(r"([0-9]+(?:\.[0-9]+)?)\s*s", line) + if m2: + dur = m2.group(1) + "s" + if not dur: + return None + idx = line.find("耗时") + action = line[:idx].strip() + if not action: + action = line.strip()[:80] + if len(action) > 80: + action = action[:77] + "..." + return (action, dur) + + def update_loop(): + if stop_flag["stop"]: + return + + # update image + try: + try: + from path_config import get_screenshot_path + + screenshot_path = get_screenshot_path() + except Exception: + import path_config + + screenshot_path = path_config.get_screenshot_path() + + if os.path.exists(screenshot_path): + img = Image.open(screenshot_path) + max_w = 480 + w, h = img.size + if w > max_w: + nw = max_w + nh = int(h * (nw / w)) + img = img.resize((nw, nh), Image.LANCZOS) + tkimg = ImageTk.PhotoImage(img) + img_lbl.configure(image=tkimg) + img_lbl.image = tkimg + else: + img_lbl.configure(image=None) + img_lbl.image = None + except Exception: + img_lbl.configure(image=None) + img_lbl.image = None + + # update minimal list + try: + path = os.path.join(get_logs_dir(), "actions.log") + lines = tail_read(path, max_bytes=64 * 1024) + items: list[str] = [] + for line in reversed(lines): + out = parse_minimal(line) + if out: + items.append(f"{out[0]} {out[1]}") + if len(items) >= 80: + break + items = list(reversed(items)) + listbox.delete(0, tk.END) + if not items: + listbox.insert(tk.END, "暂无动作(仅显示包含 耗时 的行)") + else: + for it in items: + listbox.insert(tk.END, it) + # 自动滚动到末尾 + try: + listbox.yview_moveto(1.0) + except Exception: + pass + except Exception: + try: + listbox.delete(0, tk.END) + listbox.insert(tk.END, "更新失败") + except Exception: + pass + + # schedule next + try: + win.after(1000, update_loop) + except Exception: + return + + # start + update_loop() + + except Exception: + return + # 搜索工具栏 log_search_frame = ttk.Frame(log_box) log_search_frame.pack(fill=X, padx=10, pady=(10, 5))