python - 继承和访问父级有什么问题吗?

标签 python python-2.7 class tkinter

通过继承和访问父级,小部件出现了一些奇怪的错误。我需要使用代码中所示的相同小部件制作三个部分。

我不知道如何才能做到这一点真正有意义。您知道如何以最明智的方式做到这一点吗?

import Tkinter as tk

class Interface(tk.Frame):
    def __init__(self, parent,start, ver_part, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        self.start = start
        self.ver_part = ver_part
        parent.title("Interface Window")

        self.frame = tk.LabelFrame(parent, text="Verification Part - %s"%ver_part)
        self.button = tk.Button(self.frame, text="%s"%ver_part)

        self.frame.grid(row=0+start)
        self.button.grid(row=0+start)

class Main(Interface):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        self.button_run = tk.Button(parent, text="Run", command=self.on_run())
        self.button_run.grid(row=3)

    def on_run(self):
        pass

if __name__ == "__main__":
    root = tk.Tk()
    int_a = Interface(root,0, "A")
    int_b = Interface(root,1, "B")
    int_c = Interface(root,2, "C")
    root.mainloop()

最佳答案

您正在为您的一个小部件指定 parent 的父级而不是self .

更改此行:

self.frame = tk.LabelFrame(parent, text="Verification Part - %s"%ver_part)

对此:

self.frame = tk.LabelFrame(self, text="Verification Part - %s"%ver_part)

完成后,您需要调用pack , placegridInterface 的各个实例上。

例如:

if __name__ == "__main__":
    ...
    int_a.pack(side="top", fill="both", expand=True)
    int_b.pack(side="top", fill="both", expand=True)
    int_c.pack(side="top", fill="both", expand=True)

关于python - 继承和访问父级有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39653851/

相关文章:

python - Python 的 "with"语句的 OCaml 对应项是什么(自动释放资源)

python-2.7 - 我如何获得 PyQt4.phonon?

c++ - 在二进制文件中搜索 C++

python - 从一个 txt 文件中选取部分并使用 python 复制到另一个文件

python - 添加到 numpy 字符串数组

将plt文件转换为特定格式的gcode的Python代码

python - 字符串变量作为pyplot中的 latex

python - 如何用Python检查文件夹是否为空?

C++:无法将参数 1 从子非模板类转换为父模板类

class - 是否建议在 UML 类图中与枚举类建立关联?