通过继承和访问父级,小部件出现了一些奇怪的错误。我需要使用代码中所示的相同小部件制作三个部分。
我不知道如何才能做到这一点真正有意义。您知道如何以最明智的方式做到这一点吗?
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
, place
或grid
在 Interface
的各个实例上。
例如:
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/