简体   繁体   English

ttk Notebook 无法在其他框架中的自定义框架中显示小部件(tkinter 类继承问题)

[英]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.因此,我可以将它们作为一个组添加到 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!但是,当我先将班级的master设置为其他框架,然后将master添加到ttk.Notebook中时,该选项卡总是无法显示我的小部件! 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!看来frame3从不显示内容!

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.如果我使用默认的 tk.Frame 类作为其他 tk.Frame 的 master ,它工作正常,所以我觉得我的类出了问题。 But I can't tell it out!但是我说不出来! Can anyone tell me what's going wrong here?谁能告诉我这里出了什么问题?

You've forgotten to put the brackets here path_frame2.pack() .你忘记把括号放在这里path_frame2.pack() Also, it should be tk.Frame.__init__(self, parent, *args, **kwargs) .此外,它应该是tk.Frame.__init__(self, parent, *args, **kwargs) You've forgotten to put the parent there.你忘了把parent放在那里。 Hope that's helpful!希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM