如何只允许顶部框架垂直拉伸

How to only allow the top frame verticaly stretch

我尝试采用,几乎没有修改。

import tkinter as tk
from tkinter import ttk
import random 

class App():

    def __init__(self):

        self.root = tk.Tk()

        self.frame = tk.Frame(self.root)
        self.frame.pack(expand=True, fill=tk.BOTH)

        self.tree = ttk.Treeview(self.frame, show="headings")
        self.tree.pack(expand=True, )

        self.frameBT = tk.LabelFrame(self.root,text='Buttons')
        self.frameBT.pack(expand=True, fill=tk.X)

        self.button = ttk.Button(self.frameBT, text="Fill", command=self.fill)
        self.button.pack(side=tk.BOTTOM,expand=True)

        self.fill()

        self.root.mainloop()

    def fill(self):

        if self.has_data():
            self.tree.delete(*self.tree.get_children())

        i = random.randrange(1,10)
        self.tree["columns"]=tuple([str(i) for i in range(i)])

        for col in self.tree['columns']:
            self.tree.heading(col, text="Column {}".format(col), anchor=tk.CENTER)
            self.tree.column(col, anchor=tk.CENTER)

        j = random.randrange(10)

        for j in range(j):

            self.tree.insert("", "end", values = tuple([k for k in range(i)]))

    def has_data(self):

        has_tree = self.tree.get_children()

        return True if has_tree else False


App()

目前,当我拉伸 window 垂直方向时,我得到了

我的问题是,如何将 'Button' 保持在最底部,而当我调整主 window 大小时树视图框架的底部上下移动。

您需要:

  • 通过设置 fill=tk.BOTH:
  • 使 self.tree 填充所有可用的 space
self.tree.pack(expand=True, fill=tk.BOTH)  # added fill=tk.BOTH
  • 通过删除 expand=True 来禁用 self.frameBT 的扩展:
self.frameBT.pack(fill=tk.X)  # removed expand=True