Al_Bla
Al_Bla

Reputation: 1

How to create a ttk. Notebook and add another ttk. Notebook in each tab?

My problem is in the question. Because I get this : _tkinter.TclError: can't put .!notebook.!frame.!frame.!notebook inside .!notebook.!frame.!frame, would cause management loop

from tkinter import *
from tkinter import ttk

class Agenda(Frame):
    def __init__(self, master):
        Frame.__init__(self, master=None)
        self.master = master
        self.notebook = ttk.Notebook(self.master)
        self.frames = []
        self.cadres = []
        self.horaires = ["7 heures", "8 heures", "9 heures", "10 heures", "11 heures"]
        self.notes = []
        self.frames_horaires=[]

    def creer_onglets(self, liste):
        for i in range(len(liste)):
            self.frames.append(ttk.Frame(self.notebook))
            self.notebook.add(self.frames[i], text=liste[i])
            self.notebook.pack()

            self.cadres.append(ttk.Frame(self.frames[i]))
            self.cadres[i].pack(anchor='nw', expand=True)
            self.notes.append(ttk.Notebook(self.cadres[i]))
            for j in range(len(self.notes)):
                for k in range(len(self.horaires)):
                    self.frames_horaires.append(ttk.Frame(self.notes[i]))
                    self.notes[i].add(self.frames[i], text=self.horaires[i])
                    self.notes[i].pack()

            ttk.Label(self.cadres[i], text=f"Jour {i+1} de la semaine", background='lightblue').pack()

if __name__ == '__main__':
    root = Tk()
    root.title('Test de Notebook')
    agenda = Agenda(root)
    jours = ['Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'vendredi', 'Samedi', 'Dimanche']
    agenda.creer_onglets(liste=jours)
    root.mainloop()

Upvotes: 0

Views: 59

Answers (1)

Abhijith
Abhijith

Reputation: 124

There are several problems with this code. You have two nested loops within the main loop, which is unnecessary and the index variables j and k are not accessed. Your code is creating multiple tabs for the same time slot within each notebook. self.frames_horaires gets 5 elements on each iteration of the main loop but only the element at index i is accessed to create the tab for time slot. Also you are calling pack methods for self.notebook and self.notes[i] within the loops, which is also not correct. Here is a modified code that works.

from tkinter import *
from tkinter import ttk

class Agenda(Frame):
    def __init__(self, master):
        Frame.__init__(self, master=None)
        self.master = master
        self.notebook = ttk.Notebook(self.master)
        self.frames = []
        self.cadres = []
        self.horaires = ["7 heures", "8 heures", "9 heures", "10 heures", "11 heures"]
        self.notes = []
        self.frames_horaires=[]
    def creer_onglets(self, liste):
        for i in range(len(liste)):
           self.frames.append(ttk.Frame(self.notebook))
           self.notebook.add(self.frames[i], text=liste[i])
           # self.notebook.pack()

           self.cadres.append(ttk.Frame(self.frames[i]))
           self.cadres[i].pack(anchor='nw', expand=True)
           self.notes.append(ttk.Notebook(self.cadres[i]))

           frames_horaires_list=[] # create an empty list to add frames
           for horaires in self.horaires: # looping over each time slot
               frame_horaire = ttk.Frame(self.notes[i])
               frames_horaires_list.append(frame_horaire)
               self.notes[i].add(frame_horaire,text = horaires) # adding time slot tab to notes[i], i.e, notebook within cadre frame of each tab of day
           self.frames_horaires.append(frames_horaires_list) #append the frames list to the self.frame_horaires list
           self.notes[i].pack() # pack the notes[i] frame once the time slot tabs are added
      
           ttk.Label(self.cadres[i], text=f"Jour {i+1} de la semaine", background='lightblue').pack()
        self.notebook.pack()

if __name__ == '__main__':
    root = Tk()
    root.title('Test de Notebook')
    agenda = Agenda(root)
    jours = ['Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'vendredi', 'Samedi', 'Dimanche']
    agenda.creer_onglets(liste=jours)
    root.mainloop()

Below is the screenshot:

Screenshot of the window

Upvotes: 0

Related Questions