【发布时间】:2015-06-04 00:39:28
【问题描述】:
我的 GUI 程序使用了许多按钮,这些按钮具有几个非常具体的功能,所以我正在编写一个类,将这些方法包装在一个按钮周围,这样我就不需要为每个按钮重写简单的功能。我有课:
class ToggleableButton(Button):
__isToggled = False
__root = None
__bindMap = []
# how do I correctly structure the init ?
def __init__(self, master=None, cnf={}, **kw):
Button.__init__(self, master, cnf, kw)
def bind(self, root, bindKey, func):
# doing stuff
def Toggle(self, toggle=False):
# doing stuff
我希望能够像创建按钮一样创建对象:
drawButton = ToggleableButton(frame, image=img, width=30, command=func)
drawButton = ToggleableButton(frame, image=anotherimg)
如何声明类__init__ 方法来模仿按钮的方法?目前如果我写test = ToggleableButton(frame, text="hi", width=10, command=func) 我会得到错误:
tools.py, line 59, in __init__
Button.__init__(self, master, cnf, kw)
TypeError: __init__() takes at most 3 arguments (4 given)
【问题讨论】:
标签: python class python-2.7 tkinter