0

I have about 80 input boxes and want to use a ipy.widget button to reset any input to 0.

I have this code :

import pandas as pd
import numpy as np
import ipywidgets as ipw
from ipywidgets import *

value_1 = (ipw.FloatText(value=0,
                   description='Value:',
                   disabled=False,
                   layout = {'width':'200px'}
                  ))
value_2 = (ipw.FloatText(value=0,
                   description='Value:',
                   disabled=False,
                   layout = {'width':'200px'}
                  ))
value_3 = (ipw.FloatText(value=0,
                   description='Value:',
                   disabled=False,
                   layout = {'width':'200px'}
                  ))
value_4 = (ipw.FloatText(value=0,
                   description='Value:',
                   disabled=False,
                   layout = {'width':'200px'}
                  ))
value_5 = (ipw.FloatText(value=0,
                   description='Value:',
                   disabled=False,
                   layout = {'width':'200px'}
                  ))

    
def reset_box(*args):
    value_1.value = 0
    value_2.value = 0
    value_3.value = 0
    value_4.value = 0
    value_5.value = 0

btn2.on_click(reset_box)

Which works fine to reset the input box called value_1 through value_5, with the button called btn2

I was hoping to find a more efficient way to reset all the boxes which run from product_1-->product_21, units_1-->units_21, value_1-->value_21 and prices_1-->prices_21, which isnt just inputting them all into the function.

breaker7
  • 113
  • 8
  • [mre] please, 5 boxes should suffice to demonstrate ... – Patrick Artner Mar 12 '21 at 13:24
  • Also, make a list of box references and loop through it. You're implying that right now you have individual variables named box_1, ..., box_80 or so – Mad Physicist Mar 12 '21 at 13:26
  • Not quite a dupe, but should set you on the right track: https://stackoverflow.com/q/6181935/2988730 – Mad Physicist Mar 12 '21 at 13:31
  • edited to include 5 boxes - hope this helps ? – breaker7 Mar 12 '21 at 13:32
  • Mad Physicist - thanks. Implication is unfortunately correct - I have 80 individually named boxes. I've lots to learn ! – breaker7 Mar 12 '21 at 13:33
  • 1
    `all_my_boxes = [value_1, value_2, value_3, ...]` (you would instead do `all_my_boxe.append( ipw.FloatText(value=0, ....))` and not store then in a variable each - then for a reset, do `for box in all_my_boxes: box.value = 0` – Patrick Artner Mar 12 '21 at 13:42

0 Answers0