我的计划如下:
from Tkinter import *
class TFrame(Frame):
def __init__(self, master=None, cnf={}, **kw):
Frame.__init__(self, master, cnf, kw)
if __name__ == '__main__':
root = Tk()
tf = TFrame(root)
tf.pack()
root.mainloop()
我收到此错误:
Traceback (most recent call last):
File "testFrame.py", line 11, in <module>
tf = TFrame(root)
File "testFrame.py", line 7, in __init__
Frame.__init__(self, master, cnf, kw)
TypeError: __init__() takes at most 3 arguments (4 given)
为什么我在这里TypeError
?我做了类似的事情,比如Tkinter.Label
class。
Frame类的代码在这里:
`class Frame(Widget):
"""Frame widget which may contain other widgets and can have a 3D border."""
def __init__(self, master=None, cnf={}, **kw):
"""Construct a frame widget with the parent MASTER.
Valid resource names: background, bd, bg, borderwidth, class,
colormap, container, cursor, height, highlightbackground,
highlightcolor, highlightthickness, relief, takefocus, visual, width."""
cnf = _cnfmerge((cnf, kw))
extra = ()
if 'class_' in cnf:
extra = ('-class', cnf['class_'])
del cnf['class_']
elif 'class' in cnf:
extra = ('-class', cnf['class'])
del cnf['class']
Widget.__init__(self, master, 'frame', cnf, {}, extra)`
答案 0 :(得分:3)
您需要在kw
的调用中解包Frame.__init__
。
这应该适合你。
from Tkinter import *
class TFrame(Frame):
def __init__(self, master=None, cnf={}, **kw):
Frame.__init__(self, master, cnf, **kw)
if __name__ == '__main__':
root = Tk()
tf = TFrame(root)
tf.pack()
root.mainloop()
问题是Frame
在其调用签名中使用**kw
,它需要可变数量的关键字参数,但您尝试将字典作为位置参数传递。< / p>