feat: 添加实时查看功能,支持动态展示截图与动作流
This commit is contained in:
1 parent
bffca95af0
commit
cdada11f81
1 file changed
+156
+156
@@ -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))
|
||||
|
||||
Reference in new issue
Block a user