2 回答

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊
我想我現(xiàn)在可能知道你想做什么了。
試試這個(gè)。這就是我們通常使用裝飾器的方式。
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貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊
我認(rèn)為以下部分需要修改
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
更新
我想也許你應(yīng)該是這樣的意思:
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()
或者也許是這個(gè)?:
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()
添加回答
舉報(bào)