【发布时间】:2020-08-11 13:03:35
【问题描述】:
我需要编写一个函数,它知道单击了哪个按钮来调用它。我正在使用 ipywidgets 和 jupyter notebook。
这里是一些示例代码:
import ipywidgets as widgets
button1 = 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])
单击按钮时,应将按钮 1 作为参数传递给 self_aware 函数。
例如,print('I am button 1') 或 2 就足够了
【问题讨论】:
-
也许是
print(caller.description) -
谢谢滴,解决了!
标签: python jupyter-notebook ipywidgets