How can I show a Chaco plot that is created in a running thread? I think an example will make my idea a bit clearer:
Have a look at my example code that creates a plot with 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)
if I just call the run function via
run()
the plot will show. However if I do something like
import threading
t = threading.Thread(target=run)
t.start()
the plot is unresponsive during the execution of do_something() and then it is closed. I am asking for an explanation and even more for a workaround.
First, the problem is not limited or caused by chaco. It comes from the underlying gui toolkit, properly PyQt or wx. With calling sleep, you also forbid your gui to process events. As a general rule, never do gui changes is a thread.