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

Python Tkinter嵌套ttk.Notebook无法显示三个子标签问题求解

错误原因
  • 核心bug:添加Z子标签页时传入参数错误,代码写为tab1.add(y, text="Z"),将已绑定到Y标签的y帧重复添加到标签控件。ttk.Notebook会自动将已管理的组件移动到新标签位置,导致原Y标签位失效,新建的z帧从未被加入标签控件,最终Y标签消失。
  • 布局问题:外层标签控件、各标签内容容器未设置填充扩展属性,窗口大小变化时容易出现内容裁切、显示不全的问题。
  • 冗余问题:不同标签页的画布、滚动条、下拉框变量重名覆盖,虽不直接触发报错,但不利于后续功能扩展。
修正后完整可运行代码
import tkinter as tk                    
from tkinter import ttk
  
root = tk.Tk()
root.title("Tab Widget")
root.attributes('-zoomed', True)
# 外层一级标签控件
tabControl = ttk.Notebook(root, width=400, height=220)

# 一级标签页:Tab1内嵌二级标签控件,Tab2为普通内容帧
tab1 = ttk.Notebook(tabControl)
tab2 = ttk.Frame(tabControl)
  
tabControl.add(tab1, text ='Tab 1')
tabControl.add(tab2, text ='Tab 2')
# 修正布局:用pack自适应窗口,替代固定位置的place
tabControl.pack(fill="both", expand=True, padx=2, pady=2)

# Tab2默认内容提示
ttk.Label(tab2, text="Tab 2 内容区域").pack(pady=50)


# ------------------- 二级标签X页 -------------------
x = ttk.Frame(tab1)
x_canvas = tk.Canvas(x)
# X页垂直滚动条
x_scrollbar = ttk.Scrollbar(x, orient="vertical", command=x_canvas.yview)
x_scrollable_frame = ttk.Frame(x_canvas, width = 500, height = 500)

x_scrollable_frame.bind(
    "<Configure>",
    lambda e: x_canvas.configure(
        scrollregion=x_canvas.bbox("all")
    )
)

x_canvas.create_window((0, 0), window=x_scrollable_frame, anchor="nw")
x_canvas.configure(yscrollcommand=x_scrollbar.set)

# X页下拉框组件
ttk.Combobox(x_scrollable_frame, width = 18, value=["text1", "text2"]).place(x=20, y=20)
ttk.Combobox(x_scrollable_frame, width = 18, value=["text1", "text2"]).place(x=20, y=80)
ttk.Combobox(x_scrollable_frame, width = 18, value=["text1", "text2"]).place(x=20, y=140)
ttk.Combobox(x_scrollable_frame, width = 18, value=["text1", "text2"]).place(x=20, y=200)

# 布局填充扩展
x.pack(fill="both", expand=True)
x_canvas.pack(side="left", fill="both", expand=True)
x_scrollbar.pack(side="right", fill="y")
tab1.add(x, text="X")

# ------------------- 二级标签Y页 -------------------
y = ttk.Frame(tab1)
tab1.add(y, text="Y")

y_content = ttk.Frame(y)
y_canvas = tk.Canvas(y_content)
# Y页垂直滚动条
y_scrollbar = ttk.Scrollbar(y_content, orient="vertical", command=y_canvas.yview)
y_scrollable_frame = ttk.Frame(y_canvas, width = 500, height = 500)

y_scrollable_frame.bind(
    "<Configure>",
    lambda e: y_canvas.configure(
        scrollregion=y_canvas.bbox("all")
    )
)

y_canvas.create_window((0, 0), window=y_scrollable_frame, anchor="nw")
y_canvas.configure(yscrollcommand=y_scrollbar.set)

# Y页下拉框组件
ttk.Combobox(y_scrollable_frame, width = 18, value=["text1", "text2"]).place(x=20, y=20)
ttk.Combobox(y_scrollable_frame, width = 18, value=["text1", "text2"]).place(x=20, y=80)
ttk.Combobox(y_scrollable_frame, width = 18, value=["text1", "text2"]).place(x=20, y=140)
ttk.Combobox(y_scrollable_frame, width = 18, value=["text1", "text2"]).place(x=20, y=200)

# 布局填充扩展
y_content.pack(fill="both", expand=True)
y_canvas.pack(side="left", fill="both", expand=True)
y_scrollbar.pack(side="right", fill="y")

# ------------------- 二级标签Z页 -------------------
z = ttk.Frame(tab1)
# 修正核心bug:传入z帧而非y帧
tab1.add(z, text="Z")

z_content = ttk.Frame(z)
z_canvas = tk.Canvas(z_content)
# Z页垂直滚动条
z_scrollbar = ttk.Scrollbar(z_content, orient="vertical", command=z_canvas.yview)
z_scrollable_frame = ttk.Frame(z_canvas, width = 500, height = 500)

z_scrollable_frame.bind(
    "<Configure>",
    lambda e: z_canvas.configure(
        scrollregion=z_canvas.bbox("all")
    )
)

z_canvas.create_window((0, 0), window=z_scrollable_frame, anchor="nw")
z_canvas.configure(yscrollcommand=z_scrollbar.set)

# Z页下拉框组件
ttk.Combobox(z_scrollable_frame, width = 18, value=["text1", "text2"]).place(x=20, y=20)
ttk.Combobox(z_scrollable_frame, width = 18, value=["text1", "text2"]).place(x=20, y=80)
ttk.Combobox(z_scrollable_frame, width = 18, value=["text1", "text2"]).place(x=20, y=140)
ttk.Combobox(z_scrollable_frame, width = 18, value=["text1", "text2"]).place(x=20, y=200)

# 布局填充扩展
z_content.pack(fill="both", expand=True)
z_canvas.pack(side="left", fill="both", expand=True)
z_scrollbar.pack(side="right", fill="y")

root.mainloop()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 23:51:23