from ipywidgets import widgets
options = ["1", "2","3"]
dropdown= widgets.Dropdown(options=options)
display(dropdown)
x = dropdown.value
Every time I run this the x variable is read at 1, even when I change the drop down in the notebook.
Just need the x variable to be the option that was selected in the drop down menu.
Thanks
The value of x must be updated when the dropdown value is changed. Perhaps in your code, rather than using the variable x, simply use 'dropdown.value'. Otherwise, if storing in a variable is essential, an on_change function can be used.
from ipywidgets import widgets
options = ["1", "2","3"]
dropdown = widgets.Dropdown(options=options)
x=dropdown.value
def on_change(change):
if change['type'] == 'change' and change['name'] == 'value':
x = change['new']
dropdown.observe(on_change)
display(dropdown)
This code causes the on_change function to be called each time the dropdown item is changed, causing the x variable to be updated.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.