关于python:Tk窗口返回AttributeError:__len__

Tk window returns AttributeError: __len__

在这里,我开始创建带有文本字段的TK窗口,但是每当我运行此窗口时,都会出现错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Exception in Tkinter callback
Traceback (most recent call last):
  File"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1470, in __call__
    return self.func(*args)
  File"School.py", line 31, in begin
    emulatorI=emulator()
  File"School.py", line 20, in __init__
    code.pack(self.root)
  File"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1868, in pack_configure
    + self._options(cnf, kw))
  File"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1105, in _options
    cnf = _cnfmerge(cnf)
  File"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 114, in _cnfmerge
    for c in _flatten(cnfs):
  File"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1826, in __getattr__
    return getattr(self.tk, attr)
AttributeError: __len__

来自模块内部。我究竟做错了什么?我是类的新手,所以我可能在类设置中做错了。

我正在OS 10.9上运行python 2.7

1
2
3
4
5
6
7
8
9
10
11
12
13
class emulator:
    def __init__(self):
        self.root=Tk()
        self.root.geometry("500x500")

        self.root.title("Python")

        code=Text(self.root)
        code.pack(self.root)

        self.root.mainloop()

emulatorI=emulator()


问题出在线路

1
code.pack(self.root)

pack函数通常除关键字参数外不接受任何其他参数,因此应被称为

1
code.pack()

出现非常奇怪的错误的原因是pack可以接受位置参数,该位置参数应该是选项的字典。尝试将Tk实例视为字典时,由于缺少__len__方法而导致失败。


code.pack(self.root)的用途是什么?

pack的参数是放置方向,例如侧面,填充,填充,锚点等
(请参阅http://effbot.org/tkinterbook/pack.htm)。