Home > Software engineering >  tkk Notebook doesn't display after placing it inside a class
tkk Notebook doesn't display after placing it inside a class

Time:02-20

I am trying to add a tkk notebook to an existing program of mine, but having some issues. To troubleshoot, I followed an online tutorial, and created a tkk notebook with two tabs in a barebones program:

root = tk.Tk()
root.geometry('400x300')
notebook = ttk.Notebook(root)
notebook.pack(pady=10, expand=True)

frame1 = ttk.Frame(notebook, width=400, height=280)
frame2 = ttk.Frame(notebook, width=400, height=280)

frame1.pack(fill='both', expand=True)
frame2.pack(fill='both', expand=True)

notebook.add(frame1, text='General Information')
notebook.add(frame2, text='Profile')

root.mainloop()

This worked fine, so I took it one step towards my original program by replacing the top three lines with

class Notebook_Test(tk.Frame):
    def __init__(self):
        tk.Frame.__init__(self)
        self.master.geometry('400x300')

        notebook = ttk.Notebook(self)

and the bottom one with

frame01 = Notebook_Test()
frame01.tk.mainloop()

properly indenting everything else, of course. To my surprise, these minor changes already broke it, and the window that displays is completely empty. I'm probably missing something fundamental, as I couldn't find any questions on this topic, but I'm not sure what it is that I should be looking for, so if someone could point me in the right direction, I would really appreciate it.

CodePudding user response:

You need to call pack or grid on notebook to make sure it is visible inside it's container. You also need to call pack or grid on frame01 to make that available inside the root window.

  • Related