简体   繁体   English

Python:从 ipywidgets.observe() 而不是 3 获取单个信号

[英]Python: Get single signal from ipywidgets.observe() rather than 3

Essentially, I am creating a number of Toggle Buttons using ipywidgets.本质上,我正在使用 ipywidgets 创建许多切换按钮。 When the button is clicked, I would like to add an element to a list.单击按钮时,我想将一个元素添加到列表中。 If the button is unclicked, the element is to be removed.如果未单击该按钮,则该元素将被删除。 (I have not gotten to the action yet) (我还没开始行动)

For the Toggle Button, I am using .observe() and find that each time I press a button, I am returned 3 signals.对于切换按钮,我使用.observe()并发现每次按下按钮时,都会返回 3 个信号。 {False, True, True} if clicked and {True, False, False} if unclicked. {False, True, True}如果单击,则{True, False, False}如果未单击。 I think .observe() is running 3 times each time there is a button click.我认为.observe()每次单击按钮时都会运行 3 次。 Is there any way to return just one signal or is there an error with my code?有什么方法可以只返回一个信号还是我的代码有错误?

import ipywidgets as widgets
import numpy as np
test = np.array(['test1','test2'])
def buttonArray(button_list):
    switch = [widgets.ToggleButton(description = name, value = False) for name in button_list]
    combined = widgets.HBox(switch)
    display(combined)    

    def upon_clicked(btn):
        signal = btn.owner.value
        print(signal)

    for n in range(len(button_list)):
        switch[n].observe(upon_clicked)

buttonArray(test)

See image for output when a button is pressed:按下按钮时,请参阅 output 的图像:

在此处输入图像描述

If you print(btn) in the observed function you will see that the function is being run three times.如果您在观察到的 function 中print(btn) ,您将看到 function 正在运行 3 次。 You aren't getting an array of three values, it is a function that produces a single value being run three times:你没有得到一个包含三个值的数组,它是一个 function ,它产生一个运行三次的值:

{'name': '_property_lock', 'old': traitlets.Undefined, 'new': {'value': True}, 'owner': ToggleButton(value=False, description='test1'), 'type': 'change'}

{'name': 'value', 'old': False, 'new': True, 'owner': ToggleButton(value=True, description='test1'), 'type': 'change'}

{'name': '_property_lock', 'old': {'value': True}, 'new': {}, 'owner': ToggleButton(value=True, description='test1'), 'type': 'change'}

The _property_lock attribute is being changed twice, and the the value attribute once in the middle, hence the three function calls. _property_lock属性被更改了两次,而value属性在中间更改了一次,因此三个 function 调用。

In most cases, you probably just want the middle set data.在大多数情况下,您可能只需要中间集数据。 To achieve this, you need to state the name values that get passed to the observed function, in this case names=['value'] :为此,您需要 state 传递给观察到的 function 的name值,在这种情况下names=['value']

import ipywidgets as widgets
import numpy as np
test = np.array(['test1','test2'])
def buttonArray(button_list):
    switch = [widgets.ToggleButton(description = name, value = False) for name in button_list]
    combined = widgets.HBox(switch)
    display(combined)    

    def upon_clicked(btn):
        print(btn)
        signal = btn.owner.value
#         print(signal)

    for n in range(len(button_list)):
        switch[n].observe(upon_clicked, names=['value'])  # CHANGED HERE

buttonArray(test)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM