lesolorzanov
lesolorzanov

Reputation: 3566

ipywidgets use checkbox to display or hide other widgets

I am using ipywidgets to create a short form, showing two fields, the expected width and the height of an image.

I want to add a checkbox so that if the box is checked, the information is loaded from a file instead.

If the box is checked, a text area (or file selector) should appear and a button to load the file read it and fill the text boxes.

Should I do this by observing the checkbox event? is there a way to hide a widget?

Upvotes: 2

Views: 5101

Answers (1)

ac24
ac24

Reputation: 5565

I'm not sure there is a hide function for a widget. One way to do this would be to construct an VBox for your widgets, and add the widgets as children. Then create a function/method which reassigns the children of that VBox to which ever widgets you want to show.

from ipywidgets import Checkbox, VBox

cb1 = Checkbox(description='1')
cb2 = Checkbox(description='2')
cb3 = Checkbox(description='3')

vb = VBox(children = [cb1, cb2, cb3])
top_toggle = Checkbox(description='Remove 3')

def remove_3(button):
    if button['new']:
        vb.children = [cb1, cb2]
    else:
        vb.children = [cb1, cb2, cb3]

top_toggle.observe(remove_3, names='value')
display(top_toggle)
display(vb)

Upvotes: 7

Related Questions