Multithreading and WX

Hi,

I'm trying to embed matplotlib on WX (which seems to work well for the moment). I'd like to avoid the wx mainloop() by making it run in a different thread than that of my main program.

The classical way of doing (using show() ) doesn't work. The first time, the figure becomes the main thread and my program stops running until I close the window. The second time I call it though, my program remains the main thread... Check the following code

import threading

import matplotlib
matplotlib.use('WX')
from matplotlib.pylab import *

def f():
    fig = Figure(figsize=(8,6))
    data = range(10)
    plot(data)
    show()

f()
print 'here'
f()
print 'here'
f()

The screen prints 'here' only after closing the figure with the mouse. Then second figure appears, but the screen prints 'end' without me having to close the figure like the first one. The third figure though does not appear at all!

What am I doing wrong? I know I can use the show command only once, but my program runs sequently and I need to plot data while it runs. I also tried to run this function in a different thread but the results are similar :

t = threading.Thread(target = f)
t.start()
print 'here'
tt = threading.Thread(target = f)
tt.start()
print 'end'

If the last three lines are neglected (plot only once), then it works perfectly! My main program terminates its execution and the figure stays alive until I close it. When f() is called two times though, both windows close when my program terminates...

Does anyone have an idea of how I should do this?

Thanks in advance,
Kosta

PS: I also ttried the example embedding-in-wx-2.py which seems to be exactly my case for embedding my program in a GUI. The best would be running that example in a different thread or something like that in order to avoid the mainloop(). Any ideas?

···

--

Kosta Gaitanis

Laboratoire de Télécommunications et Télédétection - TELE
Université catholique de Louvain UCL - FSA / ELEC
Bâtiment Stevin, a.156
Place du Levant, 2
B-1348 Louvain-La-Neuve, Belgium
Tel: +32 10/47.80.75
e-mail: gaitanis@...546...

Kosta,

I'm trying to embed matplotlib on WX (which seems to work well for the
moment). I'd like to avoid the wx mainloop() by making it run in a
different thread than that of my main program.

The classical way of doing (using show() ) doesn't work. The
first time, the figure becomes the main thread and my program
stops running until I close the window. The second time I call
it though, my program remains the main thread... Check the
following code

Hmm, could you explain why you want to avoid the wx mainloop,
and what you mean by this. Do want wx to process events, right?

You say you're trying to embed matplotlib on WX, but the code
you posted doesn't do this. It uses pylab, which creates a wx
App and runs its mainloop when you do show(). My guess is that
you're probably start getting the behavior you expect once that
auto-created wxapp is killed.

A simple answer would be to not use pylab, and just use a
matplotlib FigureCanvas with wx. You'd have a mainloop then (I
don't see how you avoid that!!). You could start another thread
for non-GUI processing if you wanted, or *use* the mainloop's
events (timers or data-changing events) to update the plot.

Hope that helps,

--Matt