Sna*_*ula 2 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()
Run Code Online (Sandbox Code Playgroud)
非常感谢!
如果我了解您想做什么,则可以将所有内容放到单独的函数中,并在每次单击按钮时调用该函数:
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
Run Code Online (Sandbox Code Playgroud)
一些评论:
choices)而不是list的前四个元素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()
Run Code Online (Sandbox Code Playgroud)
保存大约一半的代码。
我不知道究竟你想通过点击一个按钮选择做什么,但我能想象你要比较的choice到correct,然后做处理这些信息的东西。你可以简单地做比较b.description == correct,并作为一个建议,做什么,然后对颜色的按钮'green',如果条件True和'red'否则像这样:
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()
Run Code Online (Sandbox Code Playgroud)