这里有什么问题? (属性错误__len__)

问题描述:

import Tkinter 

class buttton(Tkinter.Button): 
    def __init__(self,frame,action=None): 
     if action==None: 
      action=self.action 
     Tkinter.Button.__init__(self,frame,command=action) 
     self.pack(frame) 
    def action(self): 
     None 


root=Tkinter.Tk() 
button=buttton(root) 
root.mainloop() 

一旦运行这个问题,其中我无法找到,我遇到了一个非常奇怪的错误任何问题......这里有什么问题? (属性错误__len__)

Traceback (most recent call last): 
    File "C:/Users/19CRF01/Desktop/ab.py", line 14, in <module> 
    button=buttton(root) 
    File "C:/Users/19CRF01/Desktop/ab.py", line 8, in __init__ 
    self.pack(frame) 
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 1940, in pack_configure 
    + self._options(cnf, kw)) 
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 1162, in _options 
    cnf = _cnfmerge(cnf) 
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 114, in _cnfmerge 
    for c in _flatten(cnfs): 
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 1898, in __getattr__ 
    return getattr(self.tk, attr) 
AttributeError: __len__ 

我会很乐意为更多任何帮助!

这里是你的问题:

self.pack(frame) 

self.pack并不需要一个框架的说法。删除frame,它应该运行正常,像这样:

self.pack() 
+0

这就是解决方案,但描述是有点儿含糊。 'pack'不需要'frame'对象,它的帮助('pack_configure(self,cnf = {},** kw)')表明它试图使用'frame'作为配置字典。 – tdelaney

+0

另外,它看起来像在OP的代码中,想法是使用'pack()'将'self'添加到'frame'。这不是必须的,因为它已经在调用Button.__ init __()'时被设置为'frame'的子元素。 –

+0

哇......谢谢......我现在要去死在耻辱...... –