Essentially, I am creating a number of Toggle Buttons using 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. {False, True, True}
if clicked and {True, False, False}
if unclicked. I think .observe()
is running 3 times each time there is a button click. 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: