使用ipywidget进行多项选择测试

时间:2016-03-03 03:34:09

标签: python jupyter jupyter-notebook ipywidgets

我对python和python笔记本都很陌生。我试图创建一个Jupyter笔记本,它将显示图像列表中的图像,并为用户提供4个选择,以便从可点击的ipywidget按钮中获取该图像。一旦用户点击他们的选择,我想用新图像替换图像,并用4个新选项重新填充按钮。

我知道如何清除图像输出并使用button.close()关闭按钮小部件,但我似乎无法弄清楚如何使用新选项重绘按钮。一旦我关闭了容器,我无法弄清楚如何循环回到顶部,因为一旦做出选择,我就会卡在on_button_clicked函数中。这是我到目前为止所拥有的,虽然我知道它还没有接近工作的地方,并且可能在这个方法中有所作为。注意我不需要使用ipywidgets,但从可点击按钮的角度来看,这似乎是一个不错的选择:

x = ['tree.jpg','house.jpg','car.jpg','door.jpg','train.jpg','moon.jpg']

choices = random.sample(x, 4)
correct = random.choice(choices)

display(Image(correct))
time.sleep(3)

button1 = widgets.Button(description = x[0])
button2 = widgets.Button(description = x[1])
button3 = widgets.Button(description = x[2])
button4 = widgets.Button(description = x[3])

container = widgets.HBox(children=[button1,button2,button3,button4])
display(container)


button1.on_click(on_button1_clicked)
button2.on_click(on_button2_clicked)
button3.on_click(on_button3_clicked)
button4.on_click(on_button4_clicked)


def on_button1_clicked(b):
     # [insert code to record choice] 
    container.close()
    clear_output()

def on_button2_clicked(b):
     # [insert code to record choice] 
    container.close()
    clear_output()

def on_button3_clicked(b):
     # [insert code to record choice] 
    container.close()
    clear_output()

def on_button4_clicked(b):
     # [insert code to record choice] 
    container.close()
    clear_output()

非常感谢!

1 个答案:

答案 0 :(得分:4)

如果我理解你想做什么,你可以将所有内容放入一个单独的函数中,并在每次单击一个按钮时调用它:

import random
import time
from IPython.display import Image, display, clear_output
from ipywidgets import widgets

x = ['tree.jpg','house.jpg','car.jpg','door.jpg','train.jpg','moon.jpg']

def redraw():
    choices = random.sample(x, 4)
    correct = random.choice(choices)

    display(Image(correct))
    time.sleep(3)

    button1 = widgets.Button(description = choices[0])
    button2 = widgets.Button(description = choices[1])
    button3 = widgets.Button(description = choices[2])
    button4 = widgets.Button(description = choices[3])

    container = widgets.HBox(children=[button1,button2,button3,button4])
    display(container)

    def on_button1_clicked(b):
        # [insert code to record choice] 
        container.close()
        clear_output()
        redraw()

    def on_button2_clicked(b):
        # [insert code to record choice] 
        container.close()
        clear_output()
        redraw()

    def on_button3_clicked(b):
        # [insert code to record choice] 
        container.close()
        clear_output()
        redraw()

    def on_button4_clicked(b):
        # [insert code to record choice] 
        container.close()
        clear_output()
        redraw()

    button1.on_click(on_button1_clicked)
    button2.on_click(on_button2_clicked)
    button3.on_click(on_button3_clicked)
    button4.on_click(on_button4_clicked)

redraw() # initializes the first choice

一些意见:

  • 我想在你想要的按钮的描述中 你选择的4个样本中的文本(即来自choices)没有 列表x的前四个元素(因为它们没有改变)。
  • 您可能不希望将选项数量硬编码为4,因为您需要硬编码4个按钮和4个按钮功能等。您可能希望根据所需的选项数生成按钮列表。类似的东西:

    nchoices = 4
    x = ['tree.jpg','house.jpg','car.jpg','door.jpg','train.jpg','moon.jpg']
    
    def redraw():    
        choices = random.sample(x, nchoices)
        correct = random.choice(choices)
    
        display(Image(correct))
        time.sleep(3)
    
        buttons = [widgets.Button(description = choice) for choice in choices]
    
        container = widgets.HBox(children=buttons)
        display(container)
    
        def on_button_clicked(b):
            # [insert code to record choice]
            container.close()
            clear_output()
            redraw()
    
        for button in buttons:
            button.on_click(on_button_clicked)
    
    redraw()
    

    节省了大约一半的代码。

  • 我不确切地知道你想通过点击一个按钮来做什么,但我可以想象你想要比较choicecorrect然后做一些事情信息。您只需b.description == correct即可进行比较,并建议如果条件为'green',则按True按钮为'red'添加颜色,其他情况如下:< / p>

    def on_button_clicked(b):
        choice = b.description
        b.color = 'white'
        b.background_color = 'green' if choice == correct else 'red'
        time.sleep(5)
        container.close()
        clear_output()
        redraw()