0

I want to create a series of ipywidgets togglebuttons to control boolean values. I defined the buttons as dictionary item values and I want to make the description of the buttons the same as the dictionary key. What I did is create the dictionary with generic buttons and the step over the items to modify the description. In my actual code the dictionary is a subset of a lager dictionary, but this illustrates the problem.

The problem that modifying the button attributes behaves different from what I expected.

To show the problem I tried the following code:

from ipywidgets import ToggleButton

test_button=dict.fromkeys(['SLM','SPM','Festo2/3', 'Vac 2/3','Orifice','Vacuum','Vac prod', 'Exhaust'],ToggleButton())
print("\ntest_button before modification:")
print("=========================")

[print(name,text) for name,text in test_button.items()]

print("\ntest_button while modifying:")
print("=========================")

for name in test_button.keys():
    test_button[name].description=name
    print(name,test_button[name])

 
print("\ntest_button after modification:")
print("=========================")    

[print(name,button) for name,button in test_button.items()]
print("=========================")

When I run this in jupyterlab I get this result:


test_button before modification:
=========================
SLM ToggleButton(value=False)
SPM ToggleButton(value=False)
Festo2/3 ToggleButton(value=False)
Vac 2/3 ToggleButton(value=False)
Orifice ToggleButton(value=False)
Vacuum ToggleButton(value=False)
Vac prod ToggleButton(value=False)
Exhaust ToggleButton(value=False)

test_button while modifying:
=========================
SLM ToggleButton(value=False, description='SLM')
SPM ToggleButton(value=False, description='SPM')
Festo2/3 ToggleButton(value=False, description='Festo2/3')
Vac 2/3 ToggleButton(value=False, description='Vac 2/3')
Orifice ToggleButton(value=False, description='Orifice')
Vacuum ToggleButton(value=False, description='Vacuum')
Vac prod ToggleButton(value=False, description='Vac prod')
Exhaust ToggleButton(value=False, description='Exhaust')

test_button after modification:
=========================
SLM ToggleButton(value=False, description='Exhaust')
SPM ToggleButton(value=False, description='Exhaust')
Festo2/3 ToggleButton(value=False, description='Exhaust')
Vac 2/3 ToggleButton(value=False, description='Exhaust')
Orifice ToggleButton(value=False, description='Exhaust')
Vacuum ToggleButton(value=False, description='Exhaust')
Vac prod ToggleButton(value=False, description='Exhaust')
Exhaust ToggleButton(value=False, description='Exhaust')
=========================

I expected the value of 'description' to be equal to the value it had while modifying, but instead it is equal to the last key value.

If I do the same thing with text item values it works as I would expect. What is fundamentally different from the following code?

test_text=dict.fromkeys(['SLM','SPM','Festo2/3', 'Vac 2/3','Orifice','Vacuum','Vac prod', 'Exhaust'],'')

for name in test_text.keys():
    test_text[name]=name
    print(name,test_text[name])
mrpraline
  • 111
  • 1
  • 3

0 Answers0