Hustler885
Hustler885

Reputation: 11

Problem with button and textbox in jupyter with ipywidgets

I am trying to initialize a variable with the text contained in a textbox when the button is pressed , in a jupyter notebook running python 3. However it seems that I can't access the value in the textbox during the button pressing event and store it for later.

Here is the code :

import ipywidgets as widgets
from IPython.display import display
button = widgets.Button(description="Click Me!")
inp = widgets.Text(description='Path:')
Box = widgets.HBox([button,inp])

def on_button_clicked(b):
    return inp.value

path = button.on_click(on_button_clicked)

display(Box

)

Any help would be greatly appreciated.Thanks in advance

Upvotes: 0

Views: 4705

Answers (2)

ac24
ac24

Reputation: 5575

The button.on_click function returns None. It doesn't return the value of the function you pass in as an argument. Think of on_click as connecting the button to a function that has side effects. In the example below I use it to append to a list.

import ipywidgets as widgets
from IPython.display import display
button = widgets.Button(description="Click Me!")
inp = widgets.Text(description='text:')
Box = widgets.HBox([button,inp])

value_list = []

def on_button_clicked(b):
    value_list.append(inp.value)
    print(value_list)

button.on_click(on_button_clicked)
Box

Upvotes: 1

Andrei R.
Andrei R.

Reputation: 321

Your value will be available through

inp.value

Please see the documentation

Upvotes: 0

Related Questions