Search code examples
tkinterresizetreeview

Tkinter treeview resizing the treeview to fit screen


I am having a small issue with getting the treeview to resize to the tkinter window and can't seem to get an answer from google. The code below works fine except for the fact it seems to have a fixed size inside the "screen". I have tried various versions of "fill" and "stretch" but I just can't seem to get it right. Can anyone shed some light on this for me?

Code:

import tkinter as tk
from tkinter import ttk

screen = tk.Tk() 
screen.title('This One')
screen.geometry('890x400')

cols = ('TOKEN', 'F-500', 'F-250', 'F-100', 'F-24', 'POS','NEG', 'NULL', 'VOLUME', 'VOLUME-FUT', 'RPP')
box = ttk.Treeview(screen, columns=cols, show='headings')
for col in cols:
    box.heading(col, text=col)
    box.grid(row=1, column=0, columnspan=2)


box.column("TOKEN", width=95)
box.column("F-500", width=85, anchor='e')
box.column("F-250", width=85, anchor='e')
box.column("F-100", width=85, anchor='e')
box.column("F-24", width=85, anchor='e')
box.column("POS", width=75, anchor='center')
box.column("NEG", width=75, anchor='center')
box.column("NULL", width=75, anchor='center')
box.column("VOLUME", width=90, anchor='center')
box.column("VOLUME-FUT", width=90, anchor='center')
box.column("RPP", width=45, anchor='center')

showScores = tk.Button(screen, text="Update", width=15).grid(row=10, column=1)
closeButton = tk.Button(screen, text="Close", width=15, command=exit).grid(row=10, column=0)

screen.mainloop()

It might be a really simple thing but I just can't see it!

Thanks,

Mort


Solution

  • Try saying this first:

    screen.grid_rowconfigure(1, weight=1)
    screen.grid_columnconfigure(0, weight=1)
    

    and make sure to also say sticky='nsew' for the treeview, like:

    box.grid(row=1, column=0, columnspan=2,sticky='nsew') #say sticky='nsew'
    

    Is this what you mean by fit to the screen? This will make sure the treeview always stays fit to the screen, when you resize the window.

    Other way would be to disable resizing of the whole window, by saying screen.resizable(0,0)

    Hope you found this answer helpful, do let me know if any errors or doubts.

    Cheers