使用 ipywidget 进行多项选择测试

Using ipywidget for multiple choice test

我对 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()

非常感谢!

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

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 进行比较,作为建议,如果条件为 True'red' 则为按钮 'green' 着色,否则像这样:

    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()