我收到一个 AttributeError 我似乎无法解决。我正在处理两个类(class)。
第一个类就是这样。
class Partie:
def __init__(self):
# deleted lines
self.interface = Interface(jeu=self)
def evaluerProposition(self):
# computations
self.interface.afficherReponse()
介绍第二类(在单独的文件中)。
class Interface:
def __init__(self, jeu):
self.jeu = jeu
self.root = tkinter.Tk()
# stuff
def onClick(self, event):
# talk
self.jeu.evaluerProposition()
def afficherReponse(self):
# stuff
我开始整件事
partie = Partie()
我的小部件上的所有操作都正常工作,直到某些点击事件导致
Exception in Tkinter callback
Traceback (cuộc gọi gần đây nhất):
File "C:\Python33\lib\tkinter\__init__.py", line 1442, in __call__
return self.func(*args)
File "C:\Users\Canard\Documents\My Dropbox\Python\AtelierPython\Mastermind\classeInterface.py", line 197, in clic
self.jeu.evaluerProposition()
File "C:\Users\Canard\Documents\My Dropbox\Python\AtelierPython\Mastermind\classeJeu.py", line 55, in evaluerProposition
self.interface.afficherReponse()
AttributeError: 'Partie' object has no attribute 'interface'
我输入了解释器
>>> dir(partie)
并得到一长串属性中的“接口(interface)”作为返回。
也打字
>>> partie.interface
所以该属性似乎存在。
按照以前的帖子中的建议,我检查了实例名称是否与模块名称一致。我很困惑。
很可能,在您没有向我们展示的某些代码中,您正在做这样的事情:
self.some_button = tkinter.Button(..., command=self.interface.onClick())
注意尾随 ()
hiện hữu onClick()
.这会导致 onClick
在创建按钮时调用的方法,这可能是在您的构造函数完成构建 Partie
的实例之前调用的方法类。
Tôi là một lập trình viên xuất sắc, rất giỏi!