python-3.xtkinterttktkinter-layoutttkwidgets

ttk Notebook tabs resizing when adding a new tab


I'm working on my first Tkinter application. It is going to have 3 different tabs along the top, each containing multiple widgets etc which are all being placed using grid. The issue I am facing is when I try to add widgets to the third tab. The first 2 I have laid out exactly as I want them, but when I start to add to the third tab, everything seems to resize and 'shrink' down.

In the small snippet of code I added below, when run without tab 3, the frame is the exact size I want it, where everything fits well for tabs 1 and 2. However when I then go and uncomment tab 3 and run it again, you can see that the whole thing shrinks and this makes the first 2 tabs look all wonky.

I've tried adding column weights to f1, f2 and f3 using grid_columnconfigure(0, weight=1) however it didn't seem to help. I also tried switching my entire layout to inner frames and pack, but it didn't seem to handle the layout I am after very well.

import tkinter as tk
import tkinter.ttk as ttk
from tkinter.ttk import Notebook
import datetime

# ~~~~~~~~~~~~~~~~~~ CREATE MAIN WINDOW AND WIDGET STYLES ~~~~~~~~~~~~~~~~~~~~
root = tk.Tk()
style = ttk.Style()
style.theme_use('clam')
root.title('Test')
root.geometry('1250x1000')
root.configure(background='yellow')

## Create a custom style for the tabs
# custom_style = ttk.Style()
# custom_style.configure('Custom.TNotebook.Tab', padding=[30,4])
tab = Notebook(root, style='Custom.TNotebook')


# ~~~~~~~~~~~~~~~~~~ CREATE & STYLE NOTEBOOK (TABS) ~~~~~~~~~~~~~~~~~~~~
## Create a frame (sub window) for each tab
f1 = tk.Frame(tab, width=700, height=700)
f2 = tk.Frame(tab, width=700, height=700)
f3 = tk.Frame(tab, width=700, height=700)

## Link the tabs to each frame
tab.add(f1, text='Tab 1')
tab.add(f2, text='Tab 2')
tab.add(f3, text='Tab 3')

## Place the Notebook (tabs) on the main window
tab.pack(fill='y')



# ~~~~~~~~~~~~~~~~~~ TAB 1 ~~~~~~~~~~~~~~~~~~~~
## Associate variables with each entry field and fill default with todays info
current_date = datetime.datetime.now()
date_var_one = tk.StringVar(value = current_date.strftime('%D'))

## Create Label and Entry Widgets
label_date = tk.ttk.Label(f1, text = 'Enter Date (MM/DD/YY): ', width = 18)

## Place Widgets on Frame (F1: Income Tab)
label_date.grid(row=0, column=0, sticky='w', pady=2)


# ~~~~~~~~~~~~~~~~~~ TAB 2 ~~~~~~~~~~~~~~~~~~~~
## Create Label and Entry Widgets
label_date_two = ttk.Label(f2,text = 'Enter Date (MM/DD/YY): ', width = 18)

## Place Widgets on Frame (F2: Expense Tab)
label_date_two.grid(row=0, column=0, sticky='w', pady=2)


# ~~~~~~~~~~~~~~~~~~ TAB 3 ~~~~~~~~~~~~~~~~~~~~
# ## Create Label and Entry widgets
# label_current = ttk.Label(f3, text = 'Enter current month (MM):',width= 18)

# ## Place Widgets on Frame (F3: Analysis Tab)
# label_current.grid(row=0, column=0)


# ~~~~~~~~~~~~~~~~~~ LOOP TO KEEP APP RUNNING ~~~~~~~~~~~~~~~~~~~~
root.mainloop()

Solution

  • The issue is not with the third tab, but that all tabs have widgets assigned and their height is recalculated.

    Try assigning the dimensions directly to the Notepad:

    tab = Notebook(root, style='Custom.TNotebook', width=700, height=700)