为什么错误不是python中的tk属性?

时间:2011-09-05 15:12:40

标签: python tkinter

为什么会出现此错误以及为何不归因?

错误:

Traceback (most recent call last):
  File "editor-new.py", line 90, in <module>
    TextEditor().mainloop()
  File "editor-new.py", line 48, in __init__
    Button(self, text='Open', command=self.onSave).pack(side=LEFT)
  File "C:\Python27_1\lib\lib-tk\Tkinter.py", line 2044, in __init__
    Widget.__init__(self, master, 'button', cnf, kw)
  File "C:\Python27_1\lib\lib-tk\Tkinter.py", line 1965, in __init__
    BaseWidget._setup(self, master, cnf)
  File "C:\Python27_1\lib\lib-tk\Tkinter.py", line 1943, in _setup
    self.tk = master.tk
AttributeError: TextEditor instance has no attribute 'tk'

代码:

http://code.google.com/p/childreneditor/source/browse/trunk/editor-new.py

有什么问题?

1 个答案:

答案 0 :(得分:1)

在此代码中:

Button(self, text='Open', command=self.onSave).pack(side=LEFT)

Button的第一个参数应该是Tkinter容器的实例(例如根窗口或框架)。根据{{​​3}},self是ScrolledText的一个实例,它不能包含其他小部件。

尝试将self更改为frm