AttributeError:lpoApp实例没有属性'数据库'

时间:2015-02-08 02:14:21

标签: python python-2.7 tkinter

我已经阅读了这个程序的几个主题,似乎常见的解决方案是不正确的缩进。我使用PEP8在线工具测试了我的代码,并且手动逐行检查并且没有发现我的缩进问题,所以我怀疑它是错误的:

AttributeError: lpoApp instance has no attribute 'database'

我现在已经看了好几个小时了,我很难过。任何见解将不胜感激。

以下是堆栈跟踪:

Traceback (most recent call last):
  File "/CodeClinic/lpoApp.py", line 213, in <module>
    main()
  File "/CodeClinic/lpoApp.py", line 209, in main
    app = lpoApp(root)
  File "/CodeClinic/lpoApp.py", line 27, in __init__
    self._createGUI()
  File "/CodeClinic/lpoApp.py", line 94, in _createGUI
    ttk.Button(self.frame_input, text='Submit', command=self._submit_callback()).grid(
  File "/CodeClinic/lpoApp.py", line 166, in _submit_callback
    data = list(self.database.get_data_for_range(start, end))
AttributeError: lpoApp instance has no attribute 'database'

让我们解决这个问题,从调用main()开始:

if __name__ == "__main__":
    main()

调用应用程序:

def main():
    root = Tk()
    app = lpoApp(root)
    root.mainloop()

现在,我们在根lpoApp窗口内创建了Tk的实例,已调用lpoApp构造函数。这是我们应该将lpoDB(已导入)self.database实例化的地方:

    def __init__(self, master):
        self.master = master
        self._createGUI()
        self.database = lpoDB.lpoDB()
        self.master.protocol("WM_DELETE_WINDOW", self._safe_close)

_createGUI()方法中,我们将self._submit_callback()绑定到Submit按钮命令。因此,让我们在那个问题上取得巅峰:

    ttk.Button(self.frame_input, text='Submit',         
              command=self._submit_callback()).grid(row=2, column=0,
              columnspan=9, padx=5)

现在,我们的Submit按钮回调了lpoApp的_submit_callback()方法:

def _submit_callback(self):
    # I printed `start` and `end` at this point prior to 
    # passing them in to the next call and they had valid
    # datetime formats that get_data_for_range is expecting

    data = list(self.database.get_data_for_range(start, end))

使用我最喜欢的IDE PyCharm,我在调试模式下运行代码,这允许我检查self的属性。 self.database未列出。因此,不知何故,解释器正在传递该声明而不是实例化我的数据库连接。

关于为什么会发生这种情况的任何想法,或者我可以做些什么来进一步测试/调试它?

2 个答案:

答案 0 :(得分:2)

此处的问题是_createGUI上的lpoApp方法在分配之前引用database

Traceback:
  ...
  File "/CodeClinic/lpoApp.py", line 27, in __init__
    self._createGUI()
  File "/CodeClinic/lpoApp.py", line 94, in _createGUI
    ttk.Button(self.frame_input, text='Submit', command=self._submit_callback()).grid(
  File "/CodeClinic/lpoApp.py", line 166, in _submit_callback
    data = list(self.database.get_data_for_range(start, end))
AttributeError: lpoApp instance has no attribute 'database'

在实例化_createGUI期间调用lpoApp后明确定义数据库连接:

def __init__(self, master):
    self.master = master
    self._createGUI()
    self.database = lpoDB.lpoDB()
    self.master.protocol("WM_DELETE_WINDOW", self._safe_close)

将您的self.database = lpoDB.lpoDB()行转移到_createGUI之上,如下所示:

def __init__(self, master):
    self.master = master
    self.database = lpoDB.lpoDB()
    self._createGUI()
    self.master.protocol("WM_DELETE_WINDOW", self._safe_close)

答案 1 :(得分:1)

基本上,在实例化名为self._createGUI()的属性之前,您正在调用self.database。这就是为什么当您将按钮Submit的命令与_submit_callback()方法相关联,并且Python正在解析时,它在任何地方都找不到self.database