You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter ttk Notebook切换删除标签页偶发内容加载缓慢问题

Tkinter ttk Notebook 删除标签页卡顿问题

问题表现

使用Tkinter的ttk Notebook组件时存在偶发异常:完成标签页切换操作后再执行删除标签页动作,Notebook需要耗费极长时间才能完成内容加载。

  • 已附上最小复现代码与问题演示动图,多次排查未定位根因
  • 曾尝试在关闭标签页时主动选中目标标签页以优化加载速度,未取得任何效果
    经测试,该问题主要出现在Mac平台,也有少量Windows用户反馈遇到同类异常。原本考虑自定义实现Notebook组件绕开问题,但自定义组件会引入其他额外问题,暂不采用该方案。

最小可复现代码

from tkinter import *
from tkinter.ttk import *
import random
window = Tk()
s = Style(window)
s.theme_use("clam")
notebook = Notebook()
notebook.pack(fill=BOTH, expand=True)
index = 0

def add_tab(event=None):
    global index
    frame = Text(notebook)
    frame.insert("1.0", "This is text {}".format(index))
    notebook.add(frame, text="Hello {}".format(index))
    index += 1
    notebook.select(frame)

def delete_tab(event=None):
    if len(notebook.tabs())>1:
        index = notebook.index("current")
        notebook.forget(notebook.select())
        if len(notebook.tabs())>index:
            notebook.select(index)

def change_tab(event=None, index=None):
    if index==None:
        number = int(event.keysym)-1
    else:
        number = index
    if len(notebook.tabs()) > number:
        notebook.select(number)

add_tab_btn = Button(window, text="Add Tab", command=add_tab)
add_tab_btn.pack(side=BOTTOM)
delete_tab_btn = Button(window, text="Delete Tab", command=delete_tab)
delete_tab_btn.pack(side=BOTTOM)

def show_issue(event=None):
    add_tab()
    add_tab()
    # 在现有标签页范围内随机切换20次标签
    tab_count = len(notebook.tabs())
    for i in range(20):
        change_tab(index=int(random.randint(0, tab_count-1)))
    delete_tab()

show_error_btn = Button(window, text="Show Error", command=show_issue)
show_error_btn.pack(side=BOTTOM)
window.bind("<Command-Key-1>", change_tab)
window.bind("<Command-Key-2>", change_tab)
window.bind("<Command-Key-3>", change_tab)
window.bind("<Command-Key-4>", change_tab)
window.bind("<Command-Key-5>", change_tab)
window.bind("<Command-Key-6>", change_tab)
window.bind("<Command-Key-7>", change_tab)
window.bind("<Command-Key-8>", change_tab)
window.bind("<Command-Key-9>", change_tab)
window.bind("<Command-t>", add_tab)

for i in range(5):
    add_tab()
window.mainloop()

问题演示

卡顿问题演示1
卡顿问题演示2

后续进展

目前已开发完成体验更好的自定义Notebook组件,可解决该卡顿问题。


内容的提问来源于stack exchange,提问作者TRCK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 07:18:25