我對python和python筆記本很新穎。我試圖創建一個Jupyter筆記本,它將顯示圖像列表中的圖像,併爲用戶提供4個可選擇的可點擊ipywidget按鈕中的圖像的選項。一旦用戶點擊他們的選擇,我想用新圖像替換圖像,並用4個新選項重新填充按鈕。使用ipywidget進行多項選擇測試
我知道如何清除圖像輸出並使用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語句也!你有沒有考慮過使用'while'循環,在條件滿足後''break's? – nluigi