简体   繁体   English

在 ttk.Notebook 的选项卡中使用小部件?

[英]Using widget in a tab of ttk.Notebook?

I'm trying to display a simple Notebook widget with two tabs.我正在尝试显示一个带有两个选项卡的简单 Notebook 小部件。 Here's the code of what I tried without all the unnecessary code in between:这是我尝试的代码,中间没有所有不必要的代码:

import tkinter as tk
from tkinter import ttk

color_bg = "gray20"
font = "Lucida Sans Typewriter"
DEFAULT_FONT_SIZE = 16

class Tab(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.parent = parent
        self.pack()

        self.textfield = tk.Text(
            self.parent, 
            font = (font, DEFAULT_FONT_SIZE), 
            background = color_bg, 
            bd = 10, 
            relief = tk.FLAT)

        self.textfield.pack(fill = tk.BOTH, expand = True)


class TabDisplay(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.parent = parent
        self.pack(fill = tk.BOTH, expand = True)

        self.tabholder = ttk.Notebook(parent)
        self.tabholder.pack(fill = tk.BOTH, expand = True, side = tk.TOP)

        self.viewtab = Tab(self.tabholder)
        self.edittab = Tab(self.tabholder)
        self.tabholder.add(self.viewtab, text = "View")
        self.tabholder.add(self.edittab, text = "Edit")


class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        # Set size and position
        self.x_pos = 0
        self.y_pos = 0
        self.width = self.parent.winfo_screenwidth()
        self.height = self.parent.winfo_screenheight()
        self.parent.geometry(f"{self.width}x{self.height}+{self.x_pos}+{self.y_pos}")
        self.parent.resizable = True

        # Add Widgets
        self.tabdisplay = TabDisplay(self)


if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root).pack(fill = tk.BOTH, expand = True)
    root.mainloop()

When I run this, it does not display an actual notebook.当我运行它时,它不显示实际的笔记本。 It just displays the two tabs below eachother.它只显示彼此下方的两个选项卡。 However, when I replace Tab(self.tabholder) with tk.Frame(self.tabholder) it functions perfectly (apart from not using the contents of the Tab() class).但是,当我用tk.Frame(self.tabholder)替换Tab(self.tabholder) ,它可以完美运行(除了不使用Tab()类的内容)。

Why does it not display correctly with my Tab() class?为什么我的Tab()类不能正确显示? I have never had issues with classes that inherit from tk.Frame until I started using ttk.Notebook() .在我开始使用ttk.Notebook()之前,我从未遇到过继承自tk.Frame类的问题。 Is it an issue with ttk?是ttk的问题吗?

EDIT: I have since found out that the actual culprit is the Text widget within the Tab() class.编辑:我后来发现真正的罪魁祸首是Tab()类中的 Text 小部件。 Now my question is why does adding a widget break the notebook?现在我的问题是为什么添加小部件会破坏笔记本?

Think of a frame like a box.把框架想象成一个盒子。 You create a box, and then you put widgets inside the box.您创建一个盒子,然后将小部件放入盒子中。 Only, in your case you're creating widgets in the box but placing them outside the box when you add them to self.parent rather than self .只是,在您的情况下,您是在框中创建小部件,但在将它们添加到self.parent而不是self时将它们放置在框外。

When you create a class that inherits from tk.Frame , every widget inside that class should be a direct child or descendant of self .当您创建一个继承自tk.Frame的类时, tk.Frame中的每个小部件都应该是self的直接子代或后代。 In doing so, an instance of the class becomes a self-contained object that can be the child of any other widget.这样做时,类的实例成为一个自包含对象,可以是任何其他小部件的子项。

For example:例如:

class Tab(tk.Frame):
    def __init__(self, parent):
        ...
        self.textfield = tk.Text(
            self, 
            ...

... and: ... 和:

class TabDisplay(tk.Frame):
    def __init__(self, parent):
        ...
        self.tabholder = ttk.Notebook(self)
        ...

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

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