Search code examples
pythontkinterttk

Can ttk notebooks be nested?


I am attempting to nest one ttk notebook within another so that I can have multiple tab levels.

Is it possible with ttk notebooks? I have not been able to find any reference or examples that deal with this question.

It seems like this code should work. I get no errors, but I can't see the second level tabs. How should I implement this? This is my solution, but it does not show the second set of tabs.

#import tkinter and ttk modules
from tkinter import *
from tkinter import ttk

#Make the root widget
root = Tk()

#Make the first notebook
nb1 = ttk.Notebook(root)
nb1.pack()
f0 = Frame(nb1)
f0.pack(expand=1, fill='both')

###Make the second notebook
nb2 = ttk.Notebook(f0)
nb2.pack()

#Make 1st tab
f1 = Frame(nb1)
#Add the tab to notebook 1
nb1.add(f1, text="First tab")

#Make 2nd tab
f2 = Frame(nb1)
#Add 2nd tab to notebook 1
nb1.add(f2, text="Second tab")

###Make 3rd tab
f3 = Frame(nb2)
#Add 3rd tab to notebook 2
nb2.add(f3, text="First tab")

###Make 4th tab
f4 = Frame(nb2)
#Add 4th tab to notebook 2
nb2.add(f4, text="Second tab")

root.mainloop()

Solution

  • Solved:

    Here is the working code simplified with notation. Hopefully others will find it instructive. This example uses the model of College Program>Terms>Courses

    #import tkinter and ttk modules
    from tkinter import *
    from tkinter import ttk
    
    #Make the root widget
    root = Tk()
    
    #Make the first notebook
    program = ttk.Notebook(root) #Create the program notebook
    program.pack()
    
    #Make the terms frames for the program notebook
    for r in range(1,4):
        termName = 'Term'+str(r) #concatenate term name(will come from dict)
        term = Frame(program)   #create frame widget to go in program nb
        program.add(term, text=termName)# add the newly created frame widget to the program notebook
        nbName=termName+'courses'#concatenate notebook name for each iter
        nbName = ttk.Notebook(term)#Create the notebooks to go in each of the terms frames
        nbName.pack()#pack the notebook
    
        for a in range (1,6):
            courseName = termName+"Course"+str(a)#concatenate coursename(will come from dict)
            course = Frame(nbName) #Create a course frame for the newly created term frame for each iter
            nbName.add(course, text=courseName)#add the course frame to the new notebook 
    
    root.mainloop()