你说得对,你必须给班级命名
tkinter.Frame
而不是仅仅
Frame
。
你也是对的,这不会解决任何问题。
因为根本问题是需要在
框架
实例
,不是在
框架
班
。
幸运的是,您已经准备好了
框架
实例,从这行:
top_Frame = tkinter.Frame(welk_window, width = "1", height = "1")
因此,您可以使用它:
self.Button_quit = tkinter.Button(top_Frame, text = "Quit", fg ="red",
command=top_Frame.quit)
但是,值得考虑一点重新设计。
通常,当您为tkinter创建类时,您希望使对象直接成为小部件,或者成为小部件的控制器。你的
Main_Frame
对象只是在初始化时创建一个框架小部件,然后忘记它,所以实际上两者都不是。
使
主框架
成为一个框架,只是继承自
框架
和使用
self
你会用到的任何地方
top_Frame
:
class Main_Frame(tkinter.Frame):
def __init__(self, welk_window):
super().__init__(welk_window, width = "1", height = "1")
self.pack()
self.Button_start = tkinter.Button(self, text = "Start", fg ="green", command=self.startMessage)
self.Button_start.pack(side=tkinter.LEFT)
self.Button_quit = tkinter.Button(self, text = "Quit", fg ="red", command=self.quit)
self.Button_quit.pack(side=tkinter.LEFT)
def startMessage(self):
print("Start")
要使其成为帧控制器,只需存储
顶部框架
作为一个属性,使用相同的方法
Button_start
:
class Main_Frame:
def __init__(self, welk_window):
self.top_Frame = tkinter.Frame(welk_window, width = "1", height = "1")
self.top_Frame.pack()
self.Button_start = tkinter.Button(top_Frame, text = "Start", fg ="green", command=self.startMessage)
self.Button_start.pack(side=tkinter.LEFT)
self.Button_quit = tkinter.Button(top_Frame, text = "Quit", fg ="red", command=self.top_Frame.quit)
self.Button_quit.pack(side=tkinter.LEFT)
def startMessage(self):
print("Start")