【问题标题】:Tk window returns AttributeError: __len__Tk 窗口返回 AttributeError: __len__
【发布时间】:2013-12-04 07:04:30
【问题描述】:

在这里,我开始创建一个带有文本字段的 TK 窗口,但是当我运行它时,我得到了错误

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

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()

【问题讨论】:

  • 请发布完整的回溯。

标签: python class tkinter tk


【解决方案1】:

问题出在线路上

code.pack(self.root)

pack 函数通常不接受除关键字参数外的任何参数,因此应调用为

code.pack()

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-23
      • 2011-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多