1

如何显示在运行线程中创建的 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()

情节将显示。但是,如果我做类似的事情

import threading
t = threading.Thread(target=run)
t.start()

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

4

1 回答 1

2

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

于 2014-05-27T21:09:48.147 回答