Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ipywidgets&;如何评估表格上的答案_Python_Events_Jupyter Notebook_Ipywidgets - Fatal编程技术网

Python ipywidgets&;如何评估表格上的答案

Python ipywidgets&;如何评估表格上的答案,python,events,jupyter-notebook,ipywidgets,Python,Events,Jupyter Notebook,Ipywidgets,我想在jupyter笔记本中添加一个表单。 为此,我的想法是: 添加RaddioButtons小部件 添加“提交答案”按钮 添加一个函数来计算答案 我的实际代码是: import ipywidgets as widgets selection = widgets.RadioButtons( options=['The color', 'The length', 'The slope'], value='The slope', description='',

我想在jupyter笔记本中添加一个表单。 为此,我的想法是:

  • 添加RaddioButtons小部件
  • 添加“提交答案”按钮
  • 添加一个函数来计算答案
我的实际代码是:

import ipywidgets as widgets

selection = widgets.RadioButtons(
    options=['The color', 'The length', 'The slope'],
    value='The slope',
    description='',
    disabled=False
)

button = widgets.Button(
    description='Submit answer',
    disabled=False,
    button_style='', # 'success', 'info', 'warning', 'danger' or ''
)

def evalute(selection):

    if (selection == "The slope"):
        print("Correct!")
    else:
        print("Try again...")
# To reset the option value. However, I would like to avoid having a "default" value
        seleccion = "The color"       

boton.on_click(evaluate(selection.value))        

left_box = widgets.VBox([selection, button])
widgets.HBox([left_box])



然而,这并不能正常工作。通过两件事:

  • 我不想要默认值
  • 我不希望“evaluate”方法在最后执行。它应该是“变化中的”
你能帮我澄清一下我的误解吗


感谢您提供了非常清晰的代码示例

1) 您可以为单选按钮指定
value=None

2) 当设置按钮上的
时,单击按钮上的
,只需给出函数名,而不实际调用函数。您正在将函数作为对象传递给按钮,单击按钮时将调用该函数(我花了一段时间才弄明白这一点)

该按钮将自身传递给
evaluate
函数,但我们真正关心的小部件是
选择器

将IPyWidget作为小部件导入
选择器=widgets.radiobutton(
选项=[“颜色”、“长度”、“坡度”],
值=无,
描述=“”,
禁用=错误
)
button=widgets.button(
description='Submit answer',
禁用=错误,
按钮样式=“”、#“成功”、“信息”、“警告”、“危险”或“”
)
def评估(按钮):
选择=选择器。获取\u交互\u值()
如果(选择=“坡度”):
打印(“正确!”)
其他:
打印(“重试…”)
#重置选项值。但是,我希望避免使用“默认”值
selector.value=None
按钮。单击(评估)
left_box=widgets.VBox([选择器,按钮])
widgets.HBox([左框])

我找到了解决办法。其想法是利用“交互式输出”功能并移除按钮。这不是我想要的,但对我来说很有效

def evaluate(a):
    if (a != None):
        if (a == "The slope"):
            print("Correct!")
        else:
            print("Try again...")
    else:
        print("Waiting for an answer")


selector = widgets.RadioButtons(
    options=['The color', 'The length', 'The slope'],
    value=None,
    description='',
    disabled=False
)

#boton = widgets.Button(
#    description='Evaluar',
#    disabled=False,
#    button_style='', # 'success', 'info', 'warning', 'danger' or ''
#)

ui = widgets.VBox([selector])
out = widgets.interactive_output(evaluar, {'a': selector})

display(ui, out)

非常感谢您的快速回复@ac24!第一点是通过您的修改修复的。但是,button小部件仍然不能正常工作,因为当我点击它时,“evaluate”函数不会打印预期的结果(“Correct”或“Try reat…”)。我还尝试用selection=selector.value替换:selection=selector.get_interact_value(),但未成功。有什么想法吗?它在ipywidgets 7.5.1版上对我有效。您使用的是哪个版本?(
widgets.\uuuu version\uuuuuu
?)与您的相同:S.7.5.1是否可以使用屏幕截图和任何错误更新原始问题?抱歉,但没有显示错误:(.单击按钮后,正确重置RadioButton的de值,但不会打印评估。