更改Tkinter中Button的命令方法时的AttributeError

时间:2012-10-16 07:56:20

标签: 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.BackButton.place(x = 30, y = 330, anchor = CENTER)

controller.py

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),但后来我收到一个错误
AttributeError:BackButton

有什么想法吗? 谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

您的错误消息显示view没有BackButton属性。 通常,您在创建self.view.BackButton之前进行操作。

提示:您的按钮似乎是在SCPIMenu中创建的,而这个按钮并非从Controller构造函数中明确调用。您可能希望将回调存储在视图中的某个位置,并在创建按钮时将其用作命令。