加载python程序时点击按钮自动调用

2024-04-25 22:25:18 发布

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

我是新来的Python3和博基。我在博基的按钮有问题。你知道吗

refresh = Button(label='refresh')

def change_click():
if refresh.label == 'refresh':
    refresh.label = 'hello world'
else:
    refresh.label = 'refresh'

refresh.on_click(change_click))

上面是我的代码,我正在用python和bokeh编写代码。 我想做的是,让按钮的标签改变每次我点击它,但标签只是改变为'你好世界'时,程序加载。当我点击按钮时,即使标签是“hello world”,它也不会变回刷新状态。 有人能告诉我为什么会发生这种情况,我该如何解决它。你知道吗


Tags: 代码helloworldifondefbutton标签
1条回答
网友
1楼 · 发布于 2024-04-25 22:25:18

请记住,缩进在python中很重要。你还有一个额外的括号。这是你所期望的

from bokeh.models import Button
from bokeh.io import curdoc

refresh = Button(label='refresh')

def change_click():
    if refresh.label == 'refresh':
        refresh.label = 'hello world'
    else:
        refresh.label = 'refresh'

refresh.on_click(change_click)

curdoc().add_root(refresh)

bokeh serve show example.py启动这个。你知道吗

这也是有效的:

from bokeh.models import Button
from bokeh.io import curdoc

refresh = Button(label='refresh')

def change_click():
    if refresh.label == 'refresh':
        refresh.update(label='hello world')
    else:
        refresh.update(label='refresh')

refresh.on_click(change_click)

curdoc().add_root(refresh)

相关问题 更多 >