我正在尝试制作一系列按钮,这些按钮根据某些场景从数据集中获取样本。我有一组 3x2 的按钮,每个按钮描述不同的场景。我似乎无法让他们执行各自的操作。
我想我了解如何将单击按钮的操作与其响应联系起来。但是,我不明白如何为多个按钮做同样的事情。
这是我的代码,它可以让一个独立的按钮工作:
button = widgets.Button(description='Generate message!')
out = widgets.Output()
def on_button_clicked(_):
samp_text = raw_data.sample(1).column(1)
# "linking function with output"
with out:
# what happens when we press the button
print(samp_text)
# linking button and function together using a button's method
button.on_click(on_button_clicked)
# displaying button and its output together
widgets.VBox([button,out])
现在我要做的是在不同的情况下采取不同类型的样本。因此,我为每种返回比例表的采样方法编写了函数:
1 47.739362
2 44.680851
3 4.920213
9 2.659574
Name: vote, dtype: float64
但是,第一个示例中只有一个按钮的相同方法不适用于多个按钮。如何使用 widgets.Output() 以及如何连接它以便单击按钮输出相应的示例摘要?
我希望单击的按钮能够输出其示例摘要,如上所示。
凤凰求蛊
尚方宝剑之说
相关分类