【问题标题】:Bokeh auto call on_click when load python program加载python程序时散景自动调用on_click
【发布时间】:2018-04-16 08:56:08
【问题描述】:

我是 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 中编码。 我想要做的是让按钮的标签在我每次单击它时都会发生变化,但是当程序加载时标签只会更改为“hello world”。当我单击按钮时,即使标签是“hello world”,它也不会变回刷新。 谁能告诉我为什么会发生这种情况以及如何解决它。

【问题讨论】:

    标签: python python-3.x button bokeh


    【解决方案1】:

    请记住,缩进在 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-14
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多