我无法循环访问下拉小部件输出
I cannot access dropdown widget output in a loop
我已经兜圈子好几个小时了。
我正在尝试让下拉输出循环,以确保结果正确。
我得到了下拉列表,但是 "output" 是 none。
如果我 select 'DEV' 或 "DEV",它会打印 DEV。输出 (w) 是 none 并且循环退出 else not if??
python代码(jypter):
source = ["Select Source", "DEV", "TEMP", "PROD"]
source_ = widgets.Dropdown(
options=source,
value=source[0],
description='Select variable:',
disabled=False,
button_style=''
)
def sourceURL(b):
clear_output()
print(source_.value)
### Drop Down
print("Drop Down")
display(source_)
w = source_.observe(sourceURL)
## print("output: ")
print(w) ### output is None
#### LOOP
if w == 'DEV':
print("This is Dev")
elif w == "TEST":
print("This is TEST")
else:
print("This is PROD")
当您执行 source_.observe(sourceURL)
时,此调用没有 return 值。因此这相当于 w = None
.
要获得您想要的行为,我认为您需要将脚本末尾的代码移动到 sourceURL
函数中。
import ipywidgets as widgets
from IPython.display import clear_output
source = ["DEV", "TEMP", "PROD"]
source_ = widgets.Dropdown(
options=source,
value=source[0],
description='Select variable:',
disabled=False,
button_style=''
)
def sourceURL(b):
# clear_output()
w = b['new']
if w == 'DEV':
print("This is Dev")
elif w == "TEMP":
print("This is TEMP")
else:
print("This is PROD")
### Drop Down
print("Drop Down")
display(source_)
w = source_.observe(sourceURL, names='value')
我已经兜圈子好几个小时了。 我正在尝试让下拉输出循环,以确保结果正确。
我得到了下拉列表,但是 "output" 是 none。 如果我 select 'DEV' 或 "DEV",它会打印 DEV。输出 (w) 是 none 并且循环退出 else not if??
python代码(jypter):
source = ["Select Source", "DEV", "TEMP", "PROD"]
source_ = widgets.Dropdown(
options=source,
value=source[0],
description='Select variable:',
disabled=False,
button_style=''
)
def sourceURL(b):
clear_output()
print(source_.value)
### Drop Down
print("Drop Down")
display(source_)
w = source_.observe(sourceURL)
## print("output: ")
print(w) ### output is None
#### LOOP
if w == 'DEV':
print("This is Dev")
elif w == "TEST":
print("This is TEST")
else:
print("This is PROD")
当您执行 source_.observe(sourceURL)
时,此调用没有 return 值。因此这相当于 w = None
.
要获得您想要的行为,我认为您需要将脚本末尾的代码移动到 sourceURL
函数中。
import ipywidgets as widgets
from IPython.display import clear_output
source = ["DEV", "TEMP", "PROD"]
source_ = widgets.Dropdown(
options=source,
value=source[0],
description='Select variable:',
disabled=False,
button_style=''
)
def sourceURL(b):
# clear_output()
w = b['new']
if w == 'DEV':
print("This is Dev")
elif w == "TEMP":
print("This is TEMP")
else:
print("This is PROD")
### Drop Down
print("Drop Down")
display(source_)
w = source_.observe(sourceURL, names='value')