pythontkinterpython-3.11

Python Tkinter Notebook gets resized when I pack a ttk.Treeview on the second tab


I am trying to write a program to time sailing races, and to simplify adding the boats to a race by selecting from a list. I have used a Treeview linked to a tk.StringVar() to filter the boats by typing a part of the person or boats name to filter the list of boats to select. This works very well. I am writing this program in Debian 12 Linux Plasma

I wanted to put a Treeview on the second tab to show the list of boats selected as entries, but when I pack this Treeview the Notebook doesn't expand to fill the window. Uncommenting the following line produces this behaviour.

#tree2.pack(fill='both', expand=True)

The following is the code

import tkinter as tk
from tkinter import ttk
import csv

# root window
root = tk.Tk()
root.geometry(str('1600x900'))

# create a notebook
notebook = ttk.Notebook(root)
notebook.pack(expand=True)

# create frames for tabs
EntriesFrame = ttk.Frame(notebook, width=1600, height=880) # 20 seems to be the right amount
RaceFrame = ttk.Frame(notebook, width=1600, height=880)

# add frames to notebook as tabs
notebook.add(EntriesFrame, text='Entries')
notebook.add(RaceFrame, text='Race')

# Set up a treeview in first tab (EntriesFrame)
ColNames = ['SailNo', 'Boat', 'HelmName', 'CrewName', 'Class', 'Fleet', 'Yardstick']
tree = ttk.Treeview(EntriesFrame, columns=ColNames, show='headings')
for ColName in ColNames:
    tree.heading(ColName, text=ColName)
tree.pack(fill='both', expand=True)

# a Treeview for the entries on the next tab.
tree2 = ttk.Treeview(RaceFrame, columns=ColNames, show='headings')
for ColName in ColNames:
    tree2.heading(ColName, text=ColName)
#tree2.pack(fill='both', expand=True)

root.mainloop()

Hope someone can help.


Solution

  • Without calling tree2.pack(...), the size of RaceFrame is still around 1600x880, so the frames inside the notebook will all have the size of the biggest frame, that is RaceFrame.

    However, when tree2.pack(...) is called, the size of RaceFrame will be shrink to the size of tree2. So the size of the notebook will also be shrink to the size that can hold the biggest frame inside it because no fill option is used in notebook.pack(...).

    Therefore adding fill='both' to notebook.pack(...) will keep the notebook to fill the size of the window. And those frames inside the notebook will also be expanded.