将 tkk Notebook 放入 class 后不显示
tkk Notebook doesn't display after placing it inside a class
我正在尝试将 tkk 笔记本添加到我现有的程序中,但遇到了一些问题。为了排除故障,我遵循了在线教程,并在准系统程序中创建了一个带有两个选项卡的 tkk 笔记本:
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()
这很好用,所以我将前三行替换为
,朝着我原来的程序迈出了一步
class Notebook_Test(tk.Frame):
def __init__(self):
tk.Frame.__init__(self)
self.master.geometry('400x300')
notebook = ttk.Notebook(self)
和底部的
frame01 = Notebook_Test()
frame01.tk.mainloop()
当然,正确缩进其他所有内容。令我惊讶的是,这些小改动 已经 破坏了它,显示的 window 完全是空的。我可能遗漏了一些基本的东西,因为我找不到关于这个主题的任何问题,但我不确定我应该寻找什么,所以如果有人能指出我正确的方向,我真的会感激不尽
您需要在 notebook
上调用 pack
或 grid
以确保它在其容器内可见。您还需要在 frame01
上调用 pack
或 grid
以使其在根 window.
中可用
我正在尝试将 tkk 笔记本添加到我现有的程序中,但遇到了一些问题。为了排除故障,我遵循了在线教程,并在准系统程序中创建了一个带有两个选项卡的 tkk 笔记本:
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()
这很好用,所以我将前三行替换为
,朝着我原来的程序迈出了一步class Notebook_Test(tk.Frame):
def __init__(self):
tk.Frame.__init__(self)
self.master.geometry('400x300')
notebook = ttk.Notebook(self)
和底部的
frame01 = Notebook_Test()
frame01.tk.mainloop()
当然,正确缩进其他所有内容。令我惊讶的是,这些小改动 已经 破坏了它,显示的 window 完全是空的。我可能遗漏了一些基本的东西,因为我找不到关于这个主题的任何问题,但我不确定我应该寻找什么,所以如果有人能指出我正确的方向,我真的会感激不尽
您需要在 notebook
上调用 pack
或 grid
以确保它在其容器内可见。您还需要在 frame01
上调用 pack
或 grid
以使其在根 window.