改变函数内的全局变量值

2024-06-16 08:28:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Jupyter笔记本中运行以下代码:

from IPython.display import display
import ipywidgets as widgets

turn_on = 'Run'
turn_off = 'Skip'

skipp = True

button_exec = widgets.Button(description=turn_on)
display(button_exec)

def exec_clicked(b):
    global skipp
    if skipp:
        b.description = turn_off
    else:
        b.description = turn_on
    skipp = not skipp

button_exec.on_click(exec_clicked)

每当我按下按钮,“skipp”的值没有改变,但是按钮的说明改变了。我试图在没有“全球”声明的情况下参选,但效果并不理想。在


Tags: 代码importondisplay笔记本jupyterbuttondescription