python : AttributeError

标签 python tkinter attributeerror

我收到一个 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 (most recent call last):
  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
<classeInterface.Interface object at 0x02C39E50>

所以该属性似乎存在。

按照以前的帖子中的建议,我检查了实例名称是否与模块名称一致。 我很困惑。

最佳答案

很可能,在您没有向我们展示的某些代码中,您正在做这样的事情:

self.some_button = tkinter.Button(..., command=self.interface.onClick())

注意尾随 ()onClick() .这会导致 onClick 在创建按钮时调用的方法,这可能是在您的构造函数完成构建 Partie 的实例之前调用的方法类。

关于 python : AttributeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17459618/

相关文章:

python - 强制进行浮点计算

python - Tkinter 变量观察器

Python 不想导入 App 类(tkinter)

python - AttributeError: 'module' 对象没有属性

python - 有MCR-ALS算法的python实现吗

python子进程的使用

python - 追踪 Python 2 中的隐式 unicode 转换

python - 在 Python Tkinter 的窗口中间的列之间放置一条网格垂直线

python - 将按钮释放视为键盘中断 tkinter

tensorflow - python3识别 tensorflow ,但不识别它的任何属性