Python 这里怎么了?(属性错误\uuuu len\uuuu)
鉴于此相对简单的tk脚本:Python 这里怎么了?(属性错误\uuuu len\uuuu),python,tkinter,Python,Tkinter,鉴于此相对简单的tk脚本: 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
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()
运行此程序时,我遇到了一个相当神秘的错误:
Traceback (most recent call last):
File "C:/Users/username/Desktop/ab.py", line 14, in <module>
button=buttton(root)
File "C:/Users/username/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__
回溯(最近一次呼叫最后一次):
文件“C:/Users/username/Desktop/ab.py”,第14行,在
按钮=巴特顿(根)
文件“C:/Users/username/Desktop/ab.py”,第8行,在初始化中__
自我包装(框架)
文件“C:\Python27\lib\lib tk\Tkinter.py”,第1940行,在pack\u configure中
+自选配件(cnf,kw))
文件“C:\Python27\lib\lib tk\Tkinter.py”,第1162行,在_选项中
cnf=_cnfmerge(cnf)
文件“C:\Python27\lib\lib-tk\Tkinter.py”,第114行,在cnfmerge中
对于c in_展平(cnfs):
文件“C:\Python27\lib\lib-tk\Tkinter.py”,第1898行,在uu getattr中__
返回getattr(self.tk,attr)
属性错误:\ n__
我将非常乐意得到任何帮助 这是您的问题:
self.pack(frame)
self.pack
不接受帧参数。移除框架
,它应该可以正常运行,如下所示:
self.pack()
这是解决方案,但描述有点模糊
pack
不接受frame
对象,它的帮助(pack\u configure(self,cnf={},**kw)
)显示它试图使用frame
作为配置字典。此外,在OP的代码中,它的想法似乎是使用pack()
将self
添加到frame
。这不是必需的,因为在调用按钮时,它已被设置为帧的子项。