2 回答
TA贡献1934条经验 获得超2个赞
我想我现在可能知道你想做什么了。
试试这个。这就是我们通常使用装饰器的方式。
class Button:
@autoclick # this is how decorators are used.
def click(self):
return False
def autoclick(self, func):
def wrapper(*args):
func(*args)# This function needes events as it argumnet
return True
if enterPressed:
return wrapper
else:
return func
TA贡献1785条经验 获得超8个赞
我认为以下部分需要修改
class TextBox:
def __init__(self, button=None):
self.button = button
def box(self):
if enterPressed:
if self.button is not None:
self.button.autoclick(self.button.click, events)()
类似于:
class TextBox:
def __init__(self, button=None):
self.button = button
def box(self):
if enterPressed:
if self.button is not None:
self.button.click = self.button.autoclick(self.button.click) # this line is changed
更新
我想也许你应该是这样的意思:
class Button:
def _click(self): # Maybe this method have events as parameters?
return False
def click(self):
return self._click()
def autoclick(self):
def wrapper(*args):
self._click(*args) # This function needs events as it arguments
self.click = self._click
return True
self.click = wrapper
class TextBox:
def __init__(self, button=None):
self.button = button
def box(self):
if enterPressed:
if self.button is not None:
self.button.autoclick()
或者也许是这个?:
class Button:
def _click(self): # Maybe this method have events as parameters?
return False
def click(self):
return self._click()
def autoclick(self):
def wrapper(*args):
result = self._click(*args) # This function needs events as it arguments
if enterPressed:
return True
return result
self.click = wrapper
class TextBox:
def __init__(self, button=None):
self.button = button
def box(self):
if self.button is not None:
self.button.autoclick()
添加回答
举报