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

Tkinter Notebook定位与布局调整:如何匹配左侧Frame尺寸实现对齐显示

问题根因

你的布局异常是由以下几个grid配置错误导致的:

  • 主窗口self.master没有给第0、1列配置权重,两个Frame拉伸时不会按比例分配宽度
  • V2frame只配置了第0列的权重,但你把Notebook放到了第1列,该列权重为0不会拉伸
  • V2frame没有给Notebook所在的行配置行权重,垂直方向不会填满容器
  • Notebook的子标签页t1没有配置列、行权重,内部的Listbox也无法填满标签页
  • V1frame没有给Listbox所在的第0行配置行权重,垂直方向的拉伸逻辑也有遗漏

修正后代码

def create_file_label(self):
    # 先给主窗口对应行列配置权重,保证两个Frame能按比例占满窗口
    self.master.columnconfigure(0, weight=1)
    self.master.columnconfigure(1, weight=1)
    self.master.rowconfigure(1, weight=1)

    # 左侧Frame逻辑修正,补全行权重配置
    V1frame=ttk.Frame(self.master)
    V1frame.grid(row=1, column=0, sticky="nsew", padx=50)
    V1frame.columnconfigure(0, weight = 1)
    V1frame.rowconfigure(0, weight=1)

    V1_list = tk.Listbox(V1frame)
    V1_list.grid(row=0, column=0, sticky="nsew")
    V1_scrollbar = tk.Scrollbar(V1frame)
    V1_scrollbar.grid(row=0, column=1, sticky="ns")
    
    V1_list.config(yscrollcommand = V1_scrollbar.set)
    V1_scrollbar.config(command = V1_list.yview)

    # 右侧Frame逻辑修正
    V2frame=ttk.Frame(self.master)
    V2frame.grid(row=1, column=1, sticky="nsew", padx=50)
    V2frame.columnconfigure(0, weight = 1)
    V2frame.rowconfigure(0, weight=1)

    FVnotebook = ttk.Notebook(V2frame)
    FVnotebook.grid(row=0, column=0, sticky="nsew")
    t1 = ttk.Frame(FVnotebook)
    FVnotebook.add(t1, text='Future Visit')
    # 给标签页配置行列权重,让内部Listbox填满标签页
    t1.columnconfigure(0, weight=1)
    t1.rowconfigure(0, weight=1)

    FV_list = tk.Listbox(t1)
    FV_list.grid(row=0, column=0, sticky="nsew")
    FV_scrollbar = tk.Scrollbar(t1)
    FV_scrollbar.grid(row=0, column=1, sticky="ns")
    
    FV_list.config(yscrollcommand=FV_scrollbar.set)
    FV_scrollbar.config(command = FV_list.yview)

    self.filelist=[V1_list, FV_list]

如果需要调整左右两个Frame的宽度比例,修改self.master.columnconfigure里的weight参数即可,比如左侧占1份、右侧占2份,就把第0列weight设为1,第1列设为2。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 17:27:04