VanesBee
VanesBee

Reputation: 616

Tkinter, treeview doesn't resize

How can I make the treeview resize when I resize the window in height ? I tried to set sticky="sn", I tried to pack the treeview with fill='y', but nothing worked.

import tkinter as tk
from tkinter.ttk import Treeview

root = tk.Tk()

f1 = tk.Frame(root)
f2 = tk.Frame(root)

f1.grid(column=0, row=0, sticky="s")
f2.grid(column=1, row=0, sticky="n")
root.rowconfigure(0, weight=1)

Treeview(f1).pack()
tk.Button(f2, text="DAT BUTTON IS IN F2").pack()
tk.Button(f2, text="DAT BUTTON IS IN F2").pack()
tk.Button(f2, text="DAT BUTTON IS IN F2").pack()

root.mainloop()

Upvotes: 1

Views: 10790

Answers (2)

Sun Bear
Sun Bear

Reputation: 8234

Here is a full grid method equivalent:

import tkinter as tk
from tkinter.ttk import Treeview

root = tk.Tk()

f1 = tk.Frame(root)
f2 = tk.Frame(root)

f1.grid(column=0, row=0, sticky="ns")
f2.grid(column=1, row=0, sticky="n")
root.rowconfigure(0, weight=1)

Treeview(f1).grid(row=0, column=0, sticky='ns')  # replaced pack with grid method
f1.rowconfigure(0, weight=1)  # Add this newline for Treeview to stretch vertically in f1. 
tk.Button(f2, text="DAT BUTTON IS IN F2").grid(row=0, column=0)
tk.Button(f2, text="DAT BUTTON IS IN F2").grid(row=1, column=0)
tk.Button(f2, text="DAT BUTTON IS IN F2").grid(row=2, column=0)

root.mainloop()

Upvotes: 0

furas
furas

Reputation: 142651

You need sticky="ns" to resize Frame in window and fill='y', expand=True to resize Treeview in Frame

import tkinter as tk
from tkinter.ttk import Treeview

root = tk.Tk()

f1 = tk.Frame(root)
f2 = tk.Frame(root)

f1.grid(column=0, row=0, sticky="ns")
f2.grid(column=1, row=0, sticky="n")
root.rowconfigure(0, weight=1)

Treeview(f1).pack(expand=True, fill='y')
tk.Button(f2, text="DAT BUTTON IS IN F2").pack()
tk.Button(f2, text="DAT BUTTON IS IN F2").pack()
tk.Button(f2, text="DAT BUTTON IS IN F2").pack()

root.mainloop()

Upvotes: 8

Related Questions