Search code examples
pythontkintertreeviewtoplevel

Tkinter toplevel: Opening treeview to fit the frame


Am having challenge letting my treeview fill the toplevel window have created have tried fill= "x" and fill="both" but not getting the result.Any suggestion to do this.

from tkinter import ttk
import tkinter as tk


def my_treeview():
    mt = tk.Toplevel()
    mt.geometry("1000x580")

   tree = ttk.Treeview(mt)
   tree.insert("", "0", "item1", text="fill width")
   tree.insert("", "1", "item2", text="fill height")

   tree.pack(fill="both")


root = tk.Tk()
root.geometry("400x400")

treeview = tk.Button(root, text="open treeview", command=my_treeview).pack()

root.mainloop()

Solution

  • fill="both" means "fill all the area that has been allocated to you". It is doing just that. The treeview widget has a certain height it wants to be, so pack allocates just enough space for it to fit. That leaves a lot of extra space that has not been allocated.

    If you want the treeview to expand to fill all remaining space and not just the space that it needs, use the expand option in addition to the fill option.

    Example:

    tree.pack(fill="both", expand=True)