来自 入库分拣工作站_源代码_v1.0.75:点商品名抖动的中间帧修复、ui/osk/tablet 调整、严格入库校验、分拣场景样本(review_queue station=picking);版本/安装包/说明同步 1.0.75。注意:本版 confirm_before_submit 恒 True、submit_on_remove 恒 False,1.0.71 的 inbound_auto_submit 账号开关被移除;models/cls 与 data/ 未入库。EXE 由用户侧打包分发,本次只推源码。
330 lines
12 KiB
Python
330 lines
12 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""「点商品名,界面抖一下」的回归自检(v1.0.75)
|
||
|
||
现场反馈:在称重页点大葱/小葱那张卡,界面看着抖。
|
||
|
||
实测(见 _merge_audit/probe_jitter.py、probe_frames.py):
|
||
· 点一下**不翻页、不滚动**,每张卡的 x/y/宽/高 一个像素都没动;
|
||
· 真正变的是两张卡「整块粉底 + 红框」在同一帧里互换 —— 190×121 ×2
|
||
约 46000 像素的大色块跳到另一张卡上,眼睛读成「闪/抖」;
|
||
· 还有一处隐患:highlightthickness 算进 Tk 的请求尺寸,反复写它会让
|
||
grid 重算列宽(实测出现过 182/198 → 190 的整屏重分配)。
|
||
|
||
v1.0.75 的改法:高亮只换 4px 边框色 + 数量数字变红(整卡粉底要
|
||
ui.focus_fill=true 才画),并且颜色没变就一个 configure 都不发;
|
||
_on_focus 里要撤的琥珀框和「旧/新确认行」合成一次 _repaint_cards。
|
||
|
||
这个脚本把上面的结论钉成断言,以后谁往点击路径里塞会改几何的东西
|
||
(wraplength、字号、padding、columnconfigure、highlightthickness…),
|
||
跑一下就能看见。用法:
|
||
|
||
python tools/_ui_jitter_check.py # 全过 → 退出码 0
|
||
|
||
断言(点两张同页的卡,来回点 3 次):
|
||
1. 每张卡的 x/y/宽/高 全程不变(不重排、不翻页);
|
||
2. 点击过程中**一次几何跳变都没有**(含跳走又跳回的);
|
||
3. grid 的列宽约束(minsize/weight/uniform)全程不变;
|
||
4. 一次点击最多重画 2 张卡,_repaint_cards 只被调用 1 次。
|
||
|
||
为什么 3 也要盯:v1.0.75 查到根因就是「列宽不统一」。点一下卡只是改
|
||
Frame 的 highlightbackground(纯颜色),Tk 却会把那张卡的请求宽度算成
|
||
198,grid 一发现总宽超了就把别的列从 190 压到 182,下次 idle 再排回
|
||
190 —— 实测 15ms 内整屏 8 张卡跳 64 次,肉眼看就是「抖」。
|
||
_layout_cards 里给列加上 uniform='cf'(所有列必须一样宽)后归零。
|
||
"""
|
||
import io
|
||
import json
|
||
import os
|
||
import sys
|
||
import time
|
||
import tkinter as tk
|
||
|
||
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
|
||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||
ROOT = os.path.dirname(HERE)
|
||
sys.path.insert(0, ROOT)
|
||
os.chdir(ROOT)
|
||
|
||
from ui import app as A # noqa: E402
|
||
|
||
BASE = json.load(open(os.path.join(ROOT, 'data', '_diag', 'demo_config.json'),
|
||
encoding='utf-8'))
|
||
BASE.setdefault('ui', {})
|
||
BASE['ui'].update({'mode': 'window', 'fit_screen': False,
|
||
'borderless': False, 'todo_thumb': False,
|
||
'width': 1440, 'height': 900})
|
||
A.load_config = lambda path='config.json', _c=BASE: _c
|
||
|
||
FAILS = []
|
||
|
||
|
||
def check(ok, what, detail=''):
|
||
print(' %s %s%s' % ('OK ' if ok else 'FAIL', what,
|
||
(' ← ' + detail) if (detail and not ok) else ''))
|
||
if not ok:
|
||
FAILS.append(what)
|
||
|
||
|
||
# ---------------- 起界面(和 _ui_offline_check 同一套假秤假相机) ----------
|
||
root = tk.Tk()
|
||
app = A.App(root)
|
||
root.geometry('1440x900+0+0')
|
||
app._login_user = '演示账号'
|
||
app._login_pwd = '123456'
|
||
app.engine.login()
|
||
t0 = time.time()
|
||
while time.time() - t0 < 10:
|
||
root.update()
|
||
if app.ready:
|
||
break
|
||
time.sleep(0.02)
|
||
|
||
|
||
def spin(sec):
|
||
t = time.time()
|
||
while time.time() - t < sec:
|
||
root.update()
|
||
time.sleep(0.01)
|
||
|
||
|
||
def wait_state(want, timeout=25.0):
|
||
t = time.time()
|
||
while time.time() - t < timeout:
|
||
root.update()
|
||
if app.engine.state == want:
|
||
return True
|
||
time.sleep(0.03)
|
||
return False
|
||
|
||
|
||
if not wait_state('CONFIRM', 25):
|
||
print('FAIL 没等到 CONFIRM 态(当前 %s),无法自检'
|
||
% getattr(app.engine, 'state', '?'))
|
||
try:
|
||
app.engine.stop()
|
||
except Exception:
|
||
pass
|
||
root.destroy()
|
||
sys.exit(1)
|
||
spin(1.0) # 让第一批卡画完、Tk 把请求尺寸算完
|
||
|
||
# ---------------- 计量工具 ----------------
|
||
EVT = [] # 点击窗口内收到的 <Configure>(路径名)
|
||
TRANS = [] # 其中「几何真变了」的瞬时跳变(含跳回去的)
|
||
LAST = {} # id(控件) → (路径, 上次几何)
|
||
FAILS = FAILS # 复用上面的失败清单
|
||
|
||
|
||
def _g(w):
|
||
return (w.winfo_x(), w.winfo_y(), w.winfo_width(), w.winfo_height())
|
||
|
||
|
||
def _walk(w, depth=0):
|
||
if depth > 4:
|
||
return
|
||
for ch in w.winfo_children():
|
||
yield ch
|
||
for x in _walk(ch, depth + 1):
|
||
yield x
|
||
|
||
|
||
def on_cfg(w):
|
||
"""计数,并按 id(控件) 比对上一次几何 —— 只数事件会漏掉「跳走又跳回」
|
||
|
||
用路径名当 key 会串味:卡片被销毁重建时 Tk 会复用 !frame38 这类名字,
|
||
新控件和老控件的老几何就对不上了,会报出假的 190→198。
|
||
"""
|
||
EVT.append(str(w))
|
||
try:
|
||
now = _g(w)
|
||
except Exception:
|
||
return
|
||
k = id(w)
|
||
prev = LAST.get(k)
|
||
LAST[k] = (str(w), now)
|
||
if prev is not None and prev[1] != now:
|
||
TRANS.append((str(w).split('.')[-1], prev[1], now))
|
||
|
||
|
||
def hook(w, depth=0):
|
||
if depth > 4:
|
||
return
|
||
try:
|
||
w.bind('<Configure>', lambda e, w=w: on_cfg(w), add='+')
|
||
except Exception:
|
||
return
|
||
for ch in w.winfo_children():
|
||
hook(ch, depth + 1)
|
||
|
||
|
||
for _c in app.todo_cards.values():
|
||
hook(_c)
|
||
|
||
|
||
def snap():
|
||
"""屏上所有卡的每个控件:路径 → (控件id, x, y, 宽, 高)
|
||
|
||
点前点后各取一份,比出「真的动了」的那些 —— 这才是肉眼看到的抖。
|
||
用路径名当 key:控件被销毁重建会换 id,也能看出来。
|
||
"""
|
||
out = {}
|
||
for rid, c in app.todo_cards.items():
|
||
try:
|
||
for w in [c] + list(_walk(c)):
|
||
out[(str(rid), str(w).split('.')[-1])] = (id(w),) + _g(w)
|
||
except Exception:
|
||
pass
|
||
return out
|
||
|
||
|
||
def diff(a, b):
|
||
out = {}
|
||
for k in set(a) & set(b):
|
||
if a[k] != b[k]:
|
||
out[k] = (a[k], b[k])
|
||
for k in set(a) - set(b):
|
||
out[k] = (a[k], '没了')
|
||
for k in set(b) - set(a):
|
||
out[k] = ('新增', b[k])
|
||
return out
|
||
|
||
|
||
def col_cfg(g):
|
||
"""当前 grid 的列配置 —— 列宽约束一变,Tk 就得把整屏重算一遍"""
|
||
out = []
|
||
for i in range(6):
|
||
try:
|
||
out.append((i, tuple(sorted(g.grid_columnconfigure(i).items()))))
|
||
except Exception:
|
||
pass
|
||
return tuple(out)
|
||
|
||
|
||
def card_req(rid):
|
||
"""一张卡的请求尺寸明细 —— 决定 grid 列宽的就是 reqwidth"""
|
||
c = app.todo_cards.get(rid)
|
||
if c is None:
|
||
return ''
|
||
bits = ['hl=%s bw=%s req=%dx%d' % (
|
||
c.cget('highlightthickness'), c.cget('borderwidth'),
|
||
c.winfo_reqwidth(), c.winfo_reqheight())]
|
||
for attr in ('_name_lbl', '_meta_lbl', '_qty_lbl', '_thumb_label'):
|
||
w = getattr(c, attr, None)
|
||
if w is None:
|
||
continue
|
||
try:
|
||
txt = str(w.cget('text'))[:18] if attr != '_thumb_label' else '图'
|
||
except Exception:
|
||
txt = '?'
|
||
bits.append('%s[%dx%d %r]' % (attr[1:], w.winfo_reqwidth(),
|
||
w.winfo_reqheight(), txt))
|
||
return ' '.join(bits)
|
||
|
||
# 统计 _repaint_cards 被调几次、每次重画几张
|
||
CALLS = []
|
||
_repaint = A.App._repaint_cards
|
||
|
||
|
||
def count_repaint(self, rids):
|
||
CALLS.append([str(r) for r in (rids or []) if r is not None])
|
||
return _repaint(self, rids)
|
||
|
||
|
||
A.App._repaint_cards = count_repaint
|
||
|
||
# ---------------- 选两张同页、名字不同的卡 ----------------
|
||
cur = str((getattr(app, '_focus_p', None) or {}).get('row_id') or '')
|
||
by_widget = {c: rid for rid, c in app.todo_cards.items()}
|
||
visible = sorted(by_widget[w] for w in app.todo_grid.grid_slaves()
|
||
if w in by_widget)
|
||
|
||
|
||
def std_of(rid):
|
||
r = (app.todo_rows.get(rid) or {})
|
||
try:
|
||
return str(app._std_name(r) or '')
|
||
except Exception:
|
||
return str(r.get('item_name') or '')
|
||
|
||
|
||
pick = [r for r in visible if r != cur]
|
||
if len(pick) < 2:
|
||
print('FAIL 同页可见卡不足 2 张(可见 %d 张),无法自检' % len(visible))
|
||
try:
|
||
app.engine.stop()
|
||
except Exception:
|
||
pass
|
||
root.destroy()
|
||
sys.exit(1)
|
||
a_rid = pick[0]
|
||
b_rid = next((r for r in pick[1:] if std_of(r) != std_of(a_rid)), pick[1])
|
||
|
||
print('state=%s 页=%s/%s 可见卡=%d 焦点=%s(%s)' % (
|
||
app.engine.state, getattr(app, '_page', 0) + 1,
|
||
getattr(app, '_page_count', 1), len(visible), cur, std_of(cur)))
|
||
print('自检用的两张卡:%s(%s) ↔ %s(%s)' % (a_rid, std_of(a_rid),
|
||
b_rid, std_of(b_rid)))
|
||
print('卡内容明细:%s' % card_req(a_rid))
|
||
|
||
g0 = snap()
|
||
p0 = getattr(app, '_page', 0)
|
||
# 先把每个控件的「当前几何」当基线,否则第一发事件都算跳变
|
||
for _rid, _c in app.todo_cards.items():
|
||
for _w in [_c] + list(_walk(_c)):
|
||
try:
|
||
LAST[id(_w)] = (str(_w), _g(_w))
|
||
except Exception:
|
||
pass
|
||
EVT.clear()
|
||
TRANS.clear()
|
||
CALLS.clear()
|
||
|
||
# ---------------- 来回点 3 次 ----------------
|
||
for i, rid in enumerate((a_rid, b_rid, a_rid), 1):
|
||
before = snap() # 点之前**当场**取,别用在循环外存的老快照
|
||
col0 = col_cfg(app.todo_grid)
|
||
c0 = len(CALLS)
|
||
t_click = time.monotonic()
|
||
EVT.clear()
|
||
TRANS.clear()
|
||
app._select_card(rid)
|
||
spin(0.35) # 足够让 Tk 把排好的布局真画出来
|
||
after = snap()
|
||
calls = CALLS[c0:]
|
||
dd = diff(before, after)
|
||
print('\n第 %d 次点击 → %s(%s),耗时 %.0fms,<Configure> %d 个,'
|
||
'其中「几何跳变」%d 次'
|
||
% (i, rid, std_of(rid), (time.monotonic() - t_click) * 1000,
|
||
len(EVT), len(TRANS)))
|
||
check(not dd, '卡片几何没动',
|
||
'\n '.join('%s.%s %s → %s' % (k[0], k[1], v[0], v[1])
|
||
for k, v in list(dd.items())[:6]))
|
||
check(not TRANS, '点击过程中没有任何控件跳位/改尺寸(含跳回去的)',
|
||
'\n '.join('%s %s → %s' % t3 for t3 in TRANS[:6]))
|
||
for nm, oldg, newg in TRANS[:6]:
|
||
print(' 跳变 %s %s → %s' % (nm, oldg, newg))
|
||
# 列宽约束:这里一变,Tk 就会把整屏 8 张卡重算一遍(190 ↔ 198/182)
|
||
col1 = col_cfg(app.todo_grid)
|
||
check(col0 == col1, 'grid 列宽约束没变(不会整屏重算)',
|
||
'%s\n → %s' % (col0, col1))
|
||
check(getattr(app, '_page', 0) == p0, '没有翻页',
|
||
'页 %s→%s' % (p0 + 1, getattr(app, '_page', 0) + 1))
|
||
check(len(calls) == 1 and len(calls[0]) <= 2,
|
||
'一次点击只重画 ≤2 张卡(_repaint_cards 1 次)',
|
||
'调用 %d 次:%s' % (len(calls), calls[:3]))
|
||
|
||
# ---------------- 收尾 ----------------
|
||
print('\n重绘面积参考(一张卡 190×121 ≈ 22990 像素):')
|
||
print(' 旧观感(整卡粉底+红框互换,ui.focus_fill=true) 每次点击≈45137 像素')
|
||
print(' 现在(只换边框+数量数字) 每次点击≈ 5617 像素')
|
||
print(' 复测旧观感:$env:FOCUS_FILL=1; python _merge_audit/probe_frames.py')
|
||
print('\n%s' % ('全部通过:点卡片不改几何、不重排' if not FAILS
|
||
else '失败 %d 项:%s' % (len(FAILS), ';'.join(FAILS))))
|
||
|
||
try:
|
||
app.engine.stop()
|
||
except Exception:
|
||
pass
|
||
root.destroy()
|
||
sys.exit(0 if not FAILS else 1)
|