繁体   English   中英

Python Bokeh:如何在子例程中更新“切换”按钮-在主菜单中定义

[英]Python Bokeh: How to update a Toggle button - defined in the main - in a subroutine

我有以下简单的bokeh示例。 启动按钮在子例程中启动一个不定式的while循环,一旦按下按钮3或取消选中该复选框,该循环应立即停止运行。 Button2会检查状态,而无需循环,这会正常工作。 由于button3和复选框cb在主目录中定义,因此button1调用的子例程无法识别更改。 有办法解决吗?

我使用了bokeh版本1.0.1。 您可以使用bokeh serve script.py完整地运行该示例,然后在浏览器( http:// localhost:5006 )中进行查看。

from bokeh.models import Column
from bokeh.plotting import curdoc
from bokeh.models.widgets import Button, Toggle, CheckboxGroup
import time

def start_loop():
    while (not button3.active) and (len(cb.active)):
        time.sleep(1)
        print(button3.active)
        print(cb.active)

def check_status():
    print(button3.active)
    print(cb.active)

button1 = Button(label = "start")
button1.on_click(start_loop)

button2 = Button(label = "check status")
button2.on_click(check_status)

button3 = Toggle(label="stop")
cb = CheckboxGroup(labels=['stop'],active=[0])

curdoc().add_root(Column(button1,button2,button3,cb))

在此处输入图片说明

我认为while循环会干扰Tornado IO_loop。 我建议您改用add_periodic_callback (Bokeh v1.1.0)

from bokeh.models import Column
from bokeh.plotting import curdoc
from bokeh.models.widgets import Button, Toggle, CheckboxGroup
import time

# def start_loop():
#     while (not button3.active) and (len(cb.active)):
#         time.sleep(1)
#         print(button3.active)
#         print(cb.active)

def check_status():
    print(button3.active)
    print(cb.active)

# button1 = Button(label = "start")
# button1.on_click(start_loop)

button2 = Button(label = "check status")
button2.on_click(check_status)

button3 = Toggle(label = "stop")
cb = CheckboxGroup(labels = ['stop'], active = [0])

curdoc().add_root(Column(button2, button3, cb))
curdoc().add_periodic_callback(check_status, 1000)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM