Button 更改Tkinter中按钮的命令方法时的AttributeError
Button 更改Tkinter中按钮的命令方法时的AttributeError,button,methods,attributes,command,tkinter,Button,Methods,Attributes,Command,Tkinter,我正在使用Tkinter开发一个Python GUI程序。在controller.py的构造函数中,我想给BackButton一个打开closeFrame函数的命令(command=self.closeFrame)。 view.py class View(TK): def SCPIMenu(self): self.BackButton = Button(self.SCPIFrame, text = "Back", command = None) self.BackButto
我正在使用Tkinter开发一个Python GUI程序。在controller.py的构造函数中,我想给BackButton一个打开closeFrame函数的命令(command=self.closeFrame)。
view.py
class View(TK):
def SCPIMenu(self):
self.BackButton = Button(self.SCPIFrame, text = "Back", command = None)
self.BackButton.place(x = 30, y = 330, anchor = CENTER)
class Controller(object):
def __init__(self):
self.view = View()
self.view.mainMenu()
self.view.mainloop()
def closeFrame(self):
self.SCPIFrame.destroy()
c = Controller()
controller.py
class View(TK):
def SCPIMenu(self):
self.BackButton = Button(self.SCPIFrame, text = "Back", command = None)
self.BackButton.place(x = 30, y = 330, anchor = CENTER)
class Controller(object):
def __init__(self):
self.view = View()
self.view.mainMenu()
self.view.mainloop()
def closeFrame(self):
self.SCPIFrame.destroy()
c = Controller()
我的想法是 self.view.BackButton.configure(command=self.closeFrame),但是我得到了一个错误
属性错误:BackButton 有什么想法吗?
谢谢您的时间。您的错误消息说
视图
没有BackButton属性。
通常,在创建self.view.BackButton之前,您正在操作它
提示:您的按钮似乎是在SCPIMenu
中创建的,它不是从控制器构造函数中明确调用的。您可能希望将回调存储在视图中的某个位置,并在创建按钮时将其用作命令。谢谢您的回答。问题是,我希望将逻辑保留在控制器类中。视图只包含GUI,没有任何函数。请不要理解您的观点,当您编写self.view.BackButton.configure(command=self.closeFrame)
时,您正在视图元素中存储对控制器函数的引用(回调的目的)。我看不出控制器执行self.view.backCallback=self.closeFrame
和更高版本(在按钮实例中)按钮(…命令=self.view.backCallback)
的区别,我现在有点困惑。在view类中,只有所有GUI元素,没有命令和函数。在控制器类中,有按钮的所有函数,在其构造函数中,我想设置按钮和GUI的命令。