2017-01-10 1 views
1

라고하는 모델이 결정 나는 내가 on_change으로 기능을 실행하려면 다음과 같은 시나리오를 가지고 :보케 : on_change 이벤트 핸들러

def update_func: 
    calling_widget = < I need the name of the widget here: "SelectorWidget" > 
    do_something 

SelectorWidget = MultiSelect(title="A widget", value = "default", options = option_list) 

SelectorWidget.on_change('value', update_func) 

같은 update_func 서로 다른 위젯에 사용됩니다, 그리고 내가 좋아하는 것 매번 함수를 트리거 한 위젯의 이름을 가져올 수 있습니다.

아이디어가 있으십니까?

답변

1

시도 :

from functools import partial 

# add a widget arg to standard callback signature 
def update_func(attr, old, new, widget): 
    # do something with widget 

SelectorWidget = MultiSelect(title="A widget", 
          value="default", 
          options=option_list) 

# use partial to make a callback callable with widget arg bound 
SelectorWidget.on_change('value', 
         partial(update_func, widget=SelectorWidget))