什么是初始化 tkinter class 的 parent/master?

What is a parent/master that initialize a tkinter class?

备注

示例代码是精简版,但可以运行具有基本功能。请关注class MainWindow(tk.Frame)

问题

什么样的object可以起到parent or master的作用进行tkinter class初始化?

在我的例子中,请参阅示例代码,为什么不能将 self 作为 parent 传递给 ProjectInfo(...)class MainWindow(tk.Frame) 中的 ConfirmItems(...)?如果使用 self 而不是 self.parent,它会弹出一个空的 window。

参考

这个问题源于我对Best way to structure a tkinter application的评论,这意味着它无法通过self作为parent

我想提出一个新问题来跟进讨论。

import tkinter as tk
from tkinter import messagebox
from collections import OrderedDict

class ProjectInfo(tk.Frame):
    def __init__(self, parent, controller, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.parent = parent
        self.controller = controller
        self.widgets_init()

    def widgets_init(self):
        tk.Label(self,
                    text = "Rock Controller!",
                    width = 10,
                    anchor = "w",
                    justify = "left").grid(row = 0, column = 0)
        tk.Label(self,
                    text = "Input Name: ").grid(row = 1, column = 0)
        self.entry = tk.Entry(self)
        self.entry.grid(row = 1, column = 1)

class ConfirmItems(tk.Frame):
    def __init__(self, parent, frames, controller, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.parent = parent
        self.frames = frames
        self.controller = controller
        self.widgets_init()

    def update_entries(self):
        self.controller.project_info.name = self.controller.project_info.entry.get()

    def update_frames(self):
        self.message = 'Click Cancel go back to reset!\n'
        for key, values in self.frames.items():
            for v in values:
                x = getattr(key, v)
                self.message += v + ': ' + str(x) + '\n'

    def show_settings(self):
        answer = tk.messagebox.askokcancel("Check Settings", self.message)
        if answer in ["yes", 1]:
            self.quit()

    def combine_funcs(self, *funcs):
        def combined_func(*args, **kwargs):
            for f in funcs:
                f(*args, **kwargs)
        return combined_func

    def widgets_init(self):
        self.cancel = tk.Button(self,
                                text = "Cancel",
                                command = self.quit)
        self.cancel.grid(row = 0, column = 0)

        self.submit = tk.Button(self,
                                text = "Submit",
                                command = self.combine_funcs(
                                                            self.update_entries,
                                                            self.update_frames,
                                                            self.show_settings))
                                # command = lambda:[self.update_frames(), self.show_settings()]
        self.submit.grid(row = 0, column = 1)

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

        self.project_info = ProjectInfo(self.parent, self.controller)
        self.project_info.grid(row = 0)
        self.widgets_init()

        self.confirm_items = ConfirmItems(self.parent, self.frames, self.controller)
        self.confirm_items.grid(row = 1)

    def widgets_init(self):
        self.dict_list = [(self.project_info, ('name',))]
        self.frames = OrderedDict(self.dict_list)

def main():
    root = tk.Tk()
    root.title("Welcome to Controller World!")
    root.geometry("300x300")
    gui = MainWindow(root)
    root.mainloop()

if __name__ == "__main__":
    main()

What kind of object can play the role of the parent or master for tkinter class initialization?

任何 tkinter 小部件都可以扮演 parent 或主人的角色,尽管通常它是根 window、顶层 window 或框架。

In my case, see the example code, why can not pass self as parent to ProjectInfo(...) or ConfirmItems(...) in class MainWindow(tk.Frame)?

您的代码中没有任何内容阻止您将 self 作为 parent 传递。

It would pop up an empty window if using self instead of self.parent.

在使用self作为ProjectInfo的parent的情况下,self指的是MainWindow的实例。我没有看到您在该实例上调用 packgrid 的任何地方。如果 parent 不可见,其 children 的 none 将可见。

您需要在 gui 变量上调用 packgrid。由于它被设计为根 window 中唯一的小部件,因此 pack 最有意义。

def main():
    ...
    gui = MainWindow(root)
    gui.pack(side="top", fill="both", expand=True)
    ...

作为一般经验法则,如果您有一个 class 从一个小部件继承并创建其他小部件,这些小部件应该始终是 children of self 或它的后代之一,而不是它自己的 parent。让 class 在其 parent 中放置 child 小部件完全违背了从 tk.Frame 继承的目的。

tk.Frame 继承的全部意义在于创建一个行为与标准小部件完全相同的新 class。您可以使用 packplacegrid 将其添加到其他小部件,它有一些标准选项,例如 borderwidthrelief,并且完全是 self-contained。