2 回答
TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超4個(gè)贊
擴(kuò)展您的示例以使用多個(gè)按鈕我沒有任何問題。我不知道你在哪里困惑。

有時(shí),小部件回調(diào)中發(fā)生的異常不會(huì)被打印出來——也許您的代碼中有一個(gè)錯(cuò)誤,因此您看不到。最好將所有內(nèi)容都包裹在“with out:”中
TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊
使用列表創(chuàng)建了兩個(gè)按鈕。猜測(cè)代碼本身解釋得更好。
from ipywidgets import Button, HBox
thisandthat = ['ON', 'OFF']
switch = [Button(description=name) for name in thisandthat]
combined = HBox([items for items in switch])
def upon_clicked(btn):
print(f'The circuit is {btn.description}.', end='\x1b\r')
for n in range(len(thisandthat)):
switch[n].style.button_color = 'gray'
btn.style.button_color = 'pink'
for n in range(len(thisandthat)):
switch[n].on_click(upon_clicked)
display(combined)
添加回答
舉報(bào)
