简体   繁体   English

如何在 function 的 output on_click

[英]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.我打算返回 gameMode 的值,因为我想使用 gameMode 的 output 对于我正在做的游戏来说是简单的中等或困难的,但总是有一个名称错误。 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.您需要从 on_click 处理程序返回一个值,或者能够设置一个属性。 Check out Return output of the function executed 'on_click'查看执行“on_click”的 function 的返回 output

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 for循环中的ipyleaflet on_click事件在每次迭代中调用function - ipyleaflet on_click event in for loop calls function on each iteration Plotly 有条件的 on_click - Plotly Conditional on_click on_click function 分配给 Streamlit 按钮在浏览器中单击事件之前运行 - on_click function assigned to Streamlit button runs before the click event in browser 如何删除所有以前注册的小部件事件处理程序(使用观察或 on_click 注册) - How to remove all previous registered handlers for widgets events (registered with observe or on_click) 如何使用带有 on_click 回调的 st.download_button 下载前处理数据? - How to process data before downloading using st.download_button with on_click callback? 如何编写on_click事件方法,该方法将为文本框设置任意值? - How do I write an on_click event method that will set an arbitrary value to a textbox? bqplot 图像的 on_click/hover 事件 - on_click/hover event for bqplot image 将 on_click 方法添加到使用 ipywidgets 创建的按钮 - adding on_click methods to buttons created with ipywidgets 将on_click选项用于REVIT RPW flexform按钮 - Using the on_click option for a REVIT RPW flexform button Plotly:在 on_click evet 上更改散点图中的标记文本 - Plotly: Change marker text in scatter plot on on_click evet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM