使用ipywidget进行多项选择测试

2024-03-28 14:59:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python和python笔记本还很陌生。我正在尝试创建一个Jupyter笔记本,它将显示一个图像列表中的图像,并给用户4个选择,该图像在可点击的ipywidget按钮中的位置。一旦用户点击他们的选择,我想用一个新的图像替换图像,并用4个新的选项重新填充按钮。在

我知道如何使用按钮.关闭(),但我似乎不知道如何用新选项重新绘制按钮。一旦我关闭了容器,我就不知道如何返回顶部,因为一旦做出选择,我就被困在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()

非常感谢!在


Tags: 图像oncontainerbuttondescriptionwidgets按钮jpg
1条回答
网友
1楼 · 发布于 2024-03-28 14:59:12

如果我明白你想做什么,你可以把所有的东西都放在一个单独的函数中,每次点击一个按钮都会调用这个函数:

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)不是 listx的前四个元素(因为这些元素不变)。在
  • 你可能不想硬编码的选择数为4,因为你硬编码4个按钮和4个按钮功能等。你可能想生成一个基于你想要的选择数量的按钮列表。比如:

    ^{pr2}$

    保存大约一半的代码。

  • 我不知道你想通过点击一个按钮来做什么,但是我可以想象你想要比较choice和{},然后用这些信息做些什么。您可以简单地通过b.description == correct进行比较,作为建议,如果条件是True和{},那么接下来要做的是给按钮'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()
    

相关问题 更多 >