简体   繁体   中英

How to the output of function on_click

I am planning to return the value of gameMode because I would like to use the output of gameMode being easy medium or hard for a game I am doing but there is always a name error. Is there any way to solve this? picture of the error: enter image description here

from ipywidgets import Button, HBox

Modes = ['Easy', 'Medium','Hard']

switch = [Button(description=name) for name in Modes]

combined = HBox([items for items in switch])

def upon_clicked(btn):
    gameMode=btn.description.lower()
    for n in range(len(Modes)):
        switch[n].style.button_color = 'gray'
    btn.style.button_color = 'pink'

for n in range(len(Modes)):
    switch[n].on_click(upon_clicked)

display(combined)
gameMode

You need to return a value from the on_click handler or be able to set an attribute. Check out Return output of the function executed 'on_click'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM