python - 为什么我的 ipywidget observe 在一次状态更改时被多次调用?

标签 python python-3.x widget jupyter ipywidgets

我在 Jupyter notebook 的单元格中有一些代码同时使用了单选按钮和 slider 。我有一个方法,我只想在选择更改时调用(在单选按钮的情况下);并且仅当 slider 被释放时(在 slider 的情况下)。

但是,当单选按钮仅更改一次时(我相信它会触发 3 次),使用“观察”方法会触发多次。当发生鼠标按下和鼠标弹起时, slider 观察方法会触发。

这可以更改为只调用一次还是我需要使用 observe 以外的东西?

[编辑] 这是使用单选按钮的更新示例以及选择一次选项时打印的输出:

import ipywidgets as widgets

    def radio_called(sender):
        print('radio_called')
        print(sender)

    radio = widgets.RadioButtons(options=['option 1', 'option2', 'option3'])
    radio.observe(radio_called)
    display(radio)

单击一次选项时的打印输出: radio_called

{'name': '_property_lock', 'old': traitlets.Undefined, 'new': {'index': 1}, 'owner': RadioButtons(options=('option 1', 'option2', 'option3'), value='option 1'), 'type': 'change'}
radio_called
{'name': 'label', 'old': 'option 1', 'new': 'option2', 'owner': RadioButtons(index=1, options=('option 1', 'option2', 'option3'), value='option 1'), 'type': 'change'}
radio_called
{'name': 'value', 'old': 'option 1', 'new': 'option2', 'owner': RadioButtons(index=1, options=('option 1', 'option2', 'option3'), value='option2'), 'type': 'change'}
radio_called
{'name': 'index', 'old': 0, 'new': 1, 'owner': RadioButtons(index=1, options=('option 1', 'option2', 'option3'), value='option2'), 'type': 'change'}
radio_called
{'name': '_property_lock', 'old': {'index': 1}, 'new': {}, 'owner': RadioButtons(index=1, options=('option 1', 'option2', 'option3'), value='option2'), 'type': 'change'}

最佳答案

如果您打印 sender 对象,您可以看到传递给该函数的内容。每个实例都是一个不同的 Trait 变化(当你点击时不仅仅是一个单一的 Action 发生),试试下面的代码。

如果您希望过滤只发生一次,请在您的 observe 调用中指定您想要的名称。例如

radio_input.observe(bind_selected_to_output, names=['value'])

    import ipywidgets as widgets # If not already imported

    output_radio_selected = widgets.Text() # Used to take the user input and access it when needed
    radio_input = widgets.RadioButtons(options=['Option 1', 'Option 2']) # Declare the set of radio buttons and provide options

    def bind_selected_to_output(sender): # Connect the input from the user to the output so we can access it
        print(sender)
        global selected_option # Global variable to hold the user input for reuse in your code
        output_radio_selected.value = radio_input.value
        selected_option = output_radio_selected.value # Example variable assigned the selected value
        print('Selected option set to: ' + selected_option) # For test purposes

    radio_input.observe(bind_selected_to_output, names=['value']) # Run the bind... function when the radio button is changed
    radio_input # Display the radio buttons to the user

查看此处了解更多信息:https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Events.html#Traitlet-events

关于python - 为什么我的 ipywidget observe 在一次状态更改时被多次调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57631700/

相关文章:

python - 您可以将一个 for 循环嵌套在另一个 for 循环中,并且它们具有相同的循环变量吗?

Android RemoteViews,如何在小部件内设置 ImageView 的 ScaleType?

ajax - Django - 基于验证的带有 "did you mean"提示的自定义小部件

jquery - jquery 小部件上的命名空间应该使用什么?

python - 使用 anaconda2 安装 python caffe (pycaffe)

python - Django 管理员 : sending signal on field change

python - "conda update --all"是否足以升级到最新的 anaconda 版本?

python - 如何验证从 Cognito CustomUI 登录页面返回的 AWS Cognito session 代码

python-3.x - 如何使用 Boto3 向 AWS Batch 提交多个命令?

eclipse - Python3 PyQt4 创建一个简单的 QCheckBox 并更改 bool 变量