如何获取函数 on_click 的输出
How to get the output of function on_click
我计划 return gameMode 的值,因为我想将 gameMode 的输出用于我正在做的游戏,简单中等或困难,但始终存在名称错误。有什么办法可以解决这个问题吗?
错误图片: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
您需要 return 来自 on_click 处理程序的值或能够设置属性。使用 作为基本示例,您可以执行以下操作:
from ipywidgets import widgets, HBox
from IPython.display import display
from traitlets import traitlets
output = widgets.Output()
class LoadedButton(widgets.Button):
"""A button that can holds a value as a attribute."""
def __init__(self, value=None, *args, **kwargs):
super(LoadedButton, self).__init__(*args, **kwargs)
# Create the value attribute.
self.add_traits(value=traitlets.Any(value))
self.style.button_color="gray"
buttons = [
LoadedButton(description="Easy", value="easy"),
LoadedButton(description="Medium", value="medium"),
LoadedButton(description="Hard", value="hard")
]
def change_btn_color(btn):
output.clear_output()
for button in buttons:
button.style.button_color="gray"
btn.style.button_color = 'pink'
with output:
print(btn.value)
for button in buttons:
button.on_click(change_btn_color)
combined = HBox(buttons)
display(combined, output)
点击按钮后,在下一个单元格中,您可以提取按钮的输出:
print(output.outputs[0]["text"])
我以前从未使用过 ipywidgets,所以希望其他人可以提出更好的解决方案,但这确实有效
我计划 return gameMode 的值,因为我想将 gameMode 的输出用于我正在做的游戏,简单中等或困难,但始终存在名称错误。有什么办法可以解决这个问题吗? 错误图片: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
您需要 return 来自 on_click 处理程序的值或能够设置属性。使用
from ipywidgets import widgets, HBox
from IPython.display import display
from traitlets import traitlets
output = widgets.Output()
class LoadedButton(widgets.Button):
"""A button that can holds a value as a attribute."""
def __init__(self, value=None, *args, **kwargs):
super(LoadedButton, self).__init__(*args, **kwargs)
# Create the value attribute.
self.add_traits(value=traitlets.Any(value))
self.style.button_color="gray"
buttons = [
LoadedButton(description="Easy", value="easy"),
LoadedButton(description="Medium", value="medium"),
LoadedButton(description="Hard", value="hard")
]
def change_btn_color(btn):
output.clear_output()
for button in buttons:
button.style.button_color="gray"
btn.style.button_color = 'pink'
with output:
print(btn.value)
for button in buttons:
button.on_click(change_btn_color)
combined = HBox(buttons)
display(combined, output)
点击按钮后,在下一个单元格中,您可以提取按钮的输出:
print(output.outputs[0]["text"])
我以前从未使用过 ipywidgets,所以希望其他人可以提出更好的解决方案,但这确实有效