在运行线程中显示chaco图

时间:2014-05-27 17:33:56

标签: python multithreading plot chaco

如何显示在正在运行的线程中创建的Chaco图?我想一个例子会让我的想法更加清晰:

看看我用Chaco创建情节的示例代码。

from traits.api import HasTraits, Instance
from traitsui.api import View, Item
from chaco.api import ArrayPlotData, Plot
from enable.component_editor import ComponentEditor

class LinePlot(HasTraits):

    plot = Instance(Plot)

    traits_view = View(
        Item('plot', editor=ComponentEditor(), 
             show_label=False
        ),
        kind='live'
    )

    def __init__(self):
        super(LinePlot, self).__init__()
        x = range(10)
        plotdata = ArrayPlotData(x=x, y=x)
        self.plot = Plot(plotdata)
        self.plot.plot(('x','y'))

def run():
    l = LinePlot()
    l.edit_traits()
    do_something()

def do_something():
    import time;time.sleep(10)

如果我只是通过

调用run函数
run()

情节将显示。但是,如果我做了像

这样的事情
import threading
t = threading.Thread(target=run)
t.start()

在执行do_something()期间,绘图没有响应,然后它被关闭。我要求解释,甚至更多的解决方法。

1 个答案:

答案 0 :(得分:2)

首先,问题不受限制或由chaco引起。它来自底层的gui工具包,正确的PyQt或wx。通过调用睡眠,您还禁止您的gui处理事件。作为一般规则,永远不要做gui更改是一个线程。