1 回答

TA貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個(gè)贊
您正在尋找getattr:
import functools
def blockSignals(*widgetnames):
def decorator(func):
@functool.wraps(func)
def method(self, *args, **kwargs):
widgets = [getattr(self.widget, name) for name in widgetnames]
for widget in widgets:
widget.blockSignals(True)
result = func(self, *args, **kwargs)
for widget in widgets:
widget.blockSignals(False)
return result
return method
return decorator
class WidgetController(...):
def __init__(...):
self.widget.myWidget.currentIndexChanged.connect(reactToChange)
@blockSignals('myWidget')
def reactToChange(...):
...
@blockSignals('anotherWidget', 'alsoBlockThisWidget')
def anotherFunction(...):
...
您必須傳遞窗口小部件的名稱,而不是窗口小部件本身,因?yàn)榉椒ㄊ窃诙x類時(shí)定義的,而不是在實(shí)例化實(shí)例時(shí)定義的。實(shí)例self和實(shí)際的窗口小部件在self.widget類定義時(shí)不存在。
該functools.wraps裝飾機(jī)拷貝原函數(shù)的名稱及其文檔字符串的裝飾功能。
添加回答
舉報(bào)