我正在使用 Tkinter 在 Python 2.7.3 中编写图形前端。我有一个主菜单(示例中的 A)和一个窗口(B)。A 包含一个列表框,B 正在做一些需要 A 中列表框内容的事情。B 完成工作后,我需要在 A 中调用一个方法(doSomething)。我的简化代码如下所示:
#!/usr/bin/env python
import Tkinter as tk
class A(object):
def __init__(self, root):
self.__mainMenu = root
self.__LB = tk.Listbox(self.__mainMenu)
self.__LB.pack()
self.__LB.insert(tk.END, "foo")
b = B(self.__mainMenu, self.__LB)
def doSomething(self):
print "Ham and spam!"
class B(object):
def __init__(self, mainMenu, LB):
self.__mainMenu = mainMenu
self.__LB = LB
print self.__LB.get(0)
self.__mainMenu.doSomething()
def main():
root = tk.Tk()
gui = A(root)
root.mainloop()
main()
产生以下输出:
$ ./myTest.py
foo
Traceback (most recent call last):
File "./myTest.py", line 29, in <module>
main()
File "./myTest.py", line 26, in main
gui = A(root)
File "./myTest.py", line 11, in __init__
b = B(self.__mainMenu, self.__LB)
File "./myTest.py", line 21, in __init__
self.__mainMenu.doSomething()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1767, in __getattr__
return getattr(self.tk, attr)
AttributeError: doSomething
这个错误来自哪里?为什么错误输出这么差?