How to let tkinter treeview fit your frame

10,593

Please replace the following line

tree.pack()

with following line

tree.pack(fill='x')

This will use the COMPLETE SPACE of ROOT screen.

Before: enter image description here After: enter image description here

Share:
10,593
AD WAN
Author by

AD WAN

Updated on June 05, 2022

Comments

  • AD WAN
    AD WAN almost 2 years

    I want my treeview to fully fit the geometry size of my window have defined for my GUI but the display doesn't cover the entire window been display in the middle.

    from tkinter import ttk
    import tkinter as tk
    
    
    root = tk.Tk()
    root.geometry("1200x680+50+20")
    
    
    tree = ttk.Treeview(root)
    tree.insert("", "0", "item1", text="LANGUAGE")
    tree.insert("", "1", "item2", text="GUI")
    tree.insert("item1", "0", text="pyhton")
    
    #SUb treeview
    style = ttk.Style(root)
    style.configure("Treeview", rowheight=70)
    tree.configure(style="Treeview")
    
        ############
    tree.config(columns=("NOTE", "book"))   # this creates to seperate headings 
    for treeview
    tree.column("NOTE", width=300)
    tree.heading("NOTE", text="Info")
    tree.column("book", width=300)
    tree.heading("book", text="profile")
    
    
    tree.set("item1", "NOTE","Am using python version 3.6.1 \n on windows 
    machine")
    tree.set("item2","NOTE","This an example Tkinter Treeview in Python, which 
    is from \nttk class make sure import ttk\n also from tkinter import *")
    
    
    tree.pack()
    root.mainloop()
    

    I tried to put it in a Frame but that doesn't also display the content for the treeview. This the code

    fr = tk.Frame(root, width=1200, height=680, relief="groove") 
    
    tree = ttk.Treeview(fr) 
    
  • AD WAN
    AD WAN over 6 years
    @ Fuzed Mass Thanks very much it means by default the pack fills the (y-axis)
  • Fuzed Mass
    Fuzed Mass over 6 years
    yes, welcome. please upvote my answer and mark is complete. thanks.
  • AD WAN
    AD WAN over 6 years
    so why cant both y and x axis be placed in the pack any reason for that. Asking this because when i change the theme if fills the x axis by default
  • Fuzed Mass
    Fuzed Mass over 6 years
    try using: tree.pack(fill='both')
  • Russell Smith
    Russell Smith over 6 years
    correction: fill=x means it will use the full width of the window, not necessarily the height.
  • Russell Smith
    Russell Smith over 6 years
    @AD_WAN: not, widgets do not by default fill the y-axis. It may appear that they do, but if you resize the window or add more widgets you'll see that widgets don't automatically fill in either direction. You simply created a widget that was too big to fit, so it looked like it filled in the y axis.
  • AD WAN
    AD WAN over 6 years
    @Bryan Oakley: thanks for the hints, but when you call the treeview as toplevel window it doesn't fill the enter window.Have used fill both and fill x any suggestion to do that.
  • Russell Smith
    Russell Smith over 6 years
    I don't know what you mean by "call the treeview as toplevel window" -- you can't "call" a treeview, and it's not a toplevel window. However, to get it to fill in both directions, use fill='both'.
  • AD WAN
    AD WAN over 6 years
    Have created a button which opens the treeview when you click on it,the treeview pops up as a toplevel. Have used the fill="both" it doesn't fill both directions.
  • AhammadaliPK
    AhammadaliPK over 3 years
    still not filling with fill="both" and fill="x" any idea?