Search code examples
pythontkinterttk

ttk Notebook cannot show widgets in a customized frame in other frames(tkinter class inheritance problem)


I created several frames, and I want to pack all of them into a main frame. Thus, I can make them as a group and add into a tab in ttk.Notebook.

However, when I set my class's master as other frames first, then add the master into ttk.Notebook, the tab always fail to display my widgets! But it works fine if I add my frame to the notebook directly.

Here is the snippet of my codes:

import tkinter as tk
from tkinter import ttk


class PathWindow(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.path_label = ttk.Label(self, text='PathWindow Label').pack()

root = tk.Tk()
nb = ttk.Notebook(root)
nb.pack()
path_frame1 = PathWindow(nb)
nb.add(path_frame1, text='path_frame1')
frame3 = tk.Frame(nb)
path_frame2 = PathWindow(frame3)
path_frame2.pack
btn3 = tk.Button(path_frame2, text='btn in path_frame2')
btn3.pack()
nb.add(frame3, text='frame3, contain path_frame2')

root.mainloop()

It appears that frame3 never show the contents!

It works fine if I use the default tk.Frame class as a master of other tk.Frame, so I feel that something goes wrong in my class. But I can't tell it out! Can anyone tell me what's going wrong here?


Solution

  • You've forgotten to put the brackets here path_frame2.pack(). Also, it should be tk.Frame.__init__(self, parent, *args, **kwargs). You've forgotten to put the parent there. Hope that's helpful!