我需要編寫(xiě)一個(gè)函數(shù),它知道單擊了哪個(gè)按鈕來(lái)調(diào)用它。我正在使用 ipywidgets 和 jupyter notebook。這是一些示例代碼:import ipywidgets as widgetsbutton1 = widgets.Button(description = 'I am button 1')button2 = widgets.Button(description = 'I am button 2')def self_aware(caller): # Some code I don't know pass button1.on_click(self_aware)button2.on_click(self_aware)widgets.VBox([button1, button2])單擊時(shí),按鈕 1 應(yīng)作為參數(shù)傳遞給函數(shù)self_aware。例如 justprint('I am button 1')或 2 就足夠了
1 回答

德瑪西亞99
TA貢獻(xiàn)1770條經(jīng)驗(yàn) 獲得超3個(gè)贊
您可以訪問(wèn)按鈕說(shuō)明:
def self_aware(caller): print(caller.description)
添加回答
舉報(bào)
0/150
提交
取消