python - Jupyter 交互 ipywidgets : several widgets for one argument

标签 python ipython ipywidgets python-interactive

是否可以将多个小部件链接到一个参数?
我有这三个 bool 小部件:

import ipywidgets as widgets
from ipywidgets import interact, fixed

w1 = widgets.Checkbox(value=False, description='Widget 1')
w2 = widgets.Checkbox(value=False, description='Widget 2')
w3 = widgets.Checkbox(value=False, description='Widget 3')

我尝试通过交互调用的函数有一个参数,该参数是三个 bool 值的列表。 例如:

def test(x, data):
   if all(data):
      # do something

现在我尝试使用交互,例如interact(test, x=fixed(3), data=[w1, w2, w3]); 但它给出了错误

'Checkbox' object is not iterable

有什么方法可以将这三个小部件链接到单个参数data吗?我必须将此参数保留为 bool 列表。

最佳答案

你可以这样做:

import ipywidgets as widgets
from ipywidgets import interact, fixed

w1 = widgets.Checkbox(value=False, description='Widget 1')
w2 = widgets.Checkbox(value=False, description='Widget 2')
w3 = widgets.Checkbox(value=False, description='Widget 3')

def test(**kwargs):
    x = kwargs.pop('val')
    if all(kwargs.values()):
        print(x)
      # do something

interact(test, val = fixed(3), w1=w1, w2=w2, w3=w3);

关于python - Jupyter 交互 ipywidgets : several widgets for one argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61909643/

相关文章:

python - 使用排序在 python 中使用命名元组进行编码

python - 无法识别不存在的文件

python - 如何在双向链表中实现插入方法?

python - 将输出写入 Azure Functions 中的 Blob

ipython - 删除多余的 ipython 内核

python - 当您将 , 运算符与 = 符号一起使用时,它有什么作用?

python - ipywidgets 使用复选框来显示或隐藏其他小部件

python - 在 ipdb shell 中使用 IPython 魔术函数

matplotlib - 使用 %matplotlib 笔记本时,图形和 ipywidget 不能位于同一代码单元中

Python ipywidgets : create a savefig button