Search code examples
pythonuser-interfacetkintergridtabcontrol

ttk Notebook not expanding with grid geometry manager


I cannot work out how to have a ttk Notebook widget expand correctly when the GUI is resized using the .grid method in tk. It is straightforward in .pack, I have tried some sticky configuration methods and they don't see to do anything useful.

Code

import tkinter as tk                     
from tkinter import ttk 

root = tk.Tk() 
root.title("Tab Widget")
root.geometry("500x350") 
tabControl = ttk.Notebook(root) 
  
tab1 = ttk.Frame(tabControl) 
tab2 = ttk.Frame(tabControl)
 
tabControl.add(tab1, text ='Tab 1') 
tabControl.add(tab2, text ='Tab 2')

tabControl.pack(expand = 1, fill ="both")
#tabControl.grid(column=0, row=0, sticky= tk.E+tk.W+tk.N+tk.S)

root.mainloop() 

Desired Outcome

A gui that can be stretched and have the tab control and frame resize with the new window size.


Solution

  • Grids, by default, shrink. If you more pack-like behavior, you must give the cells 'weight.' It's pretty easy to do:

    import tkinter as tk                     
    from tkinter import ttk 
    
    root = tk.Tk() 
    root.title("Tab Widget")
    root.geometry("500x350") 
    tabControl = ttk.Notebook(root) 
      
    tab1 = ttk.Frame(tabControl) 
    tab2 = ttk.Frame(tabControl)
     
    tabControl.add(tab1, text ='Tab 1') 
    tabControl.add(tab2, text ='Tab 2')
    
    tk.Grid.rowconfigure(root, 0, weight=1)
    tk.Grid.columnconfigure(root, 0, weight=1)
    tabControl.grid(column=0, row=0, sticky=tk.E+tk.W+tk.N+tk.S)
    
    root.mainloop() 
    

    That should do it. Let us know if you have more questions etc.