[PyKDE] How to do *INTERACTIVE* plotting with PyQt (or PyQwt)

Peter Lipa porl3141 at hotmail.com
Sat Sep 7 03:23:01 BST 2002


Hi Guys (Phil, Gerard and Boudewijn),
Thanks so much for your replies - I really appreciate any help or
suggestion. Unfortunatley I am still desparate....
I seems to me, that only Gerard Vermeulen understood fully what I *need* to
do (since he tried  that before).
I looked a bit at eric and this is not quite what I want (it is a qt app
with a single qApp in the main thread and runs
a independent - and isolated- python shell inside which in turn can run a
single Qt app to be debugged).
My problem is the other way round. I want to run several Qt windows and send
commands via the interpreter to these
windows, such as, add a line (xarray,yarray) to the current axes ).

I attached one of my many experiments to demonstrate EXACTLY what I need to
solve and what the problem seems to be.
I know that debugging someone elses code is not fun, but since this is a
very fundamental problem of general interest (interactive
scientific plotting with pyQt widgets from within the interpeter!) I hope to
find someone with more knowledge about the inner workings of
Qt, sip and python/Qt threading to help me in tackling this problem.
Anybody who spends time to crack this problem and gets me closer to a viable
solution (hopefully with example code) will get a personal Thank-You
postcard from Tucson, Arizona!!! (I rather would invite you to a few beers
together, but I can't afford the plane ticket...).

The attached appThread5.py is a variation of the application.py example that
ships with pyQt 3.3.x:
This app allows you to create as many copies of the QMainWindow as you want
by selcting File->New.
(each Main window should finally evolve into a plot with multiple axes and
graphs and interactive editing capabilities
via menu commands etc. - in short each window should be an application and
have a icon in the windows task bar)
Usually, if you run application.py from within the python interpreter, the
command line is blocked till you
quit the app (as with any other pyqt app). I put all the QApplication and
exec_loop() stuff in a function called main()
and run this in a python thread (I tried this also in a QThread but the
QThreaq.run() never gave control back to the python
interpreter! QTheads seem to need the eventloop running BEFORE creation
although I am not sure about that).

THE PART THAT SEEMS TO WORK (SO FAR):
====================================
To see where I am and what my problem is, do the following:

>>> import appThread5 as app

this brings up the familiar application with a QTextEdit central widget
titled 'Document 1' and gives control back to the python interpreter so
you can examine all runtime variables as well as send some primitive
commands to the 'Document 1' window. The qApp and Qt event loop are running
in a python thread (Thread-1) and seem to do fine (the app is working just
as it would run from the python main thread - it appears).
Every 10 secs a message (e.g x00 msgThread Thread-2 alive and listening ...)
is written from a python message thread to the edit window to tell that the
tread is alive and targeting.
Now you can send a message from the python command line to the currently
active QTextEdit widget via the command line:

>>> app.sendMsg("print blahblah")
>>> app.sendMsg("print more stuff")

the messages will appear in the window.
Now create more copies of the Mainwindow via the File->New menu item:
'Document 2', 'Document 3', etc..
The last created window is (by construction) the 'CURRENT' window (lateron
the 'current figure window to accept the plot commands given at
the command line such as xlabel("x-axis") or title("figuretitle"). The
message thread sends messages only to the CURRENT window, e.g.:

>>> app.sendMsg("print this should appear in the last Document window!')

is printed in the last created Window.
If you close the last created Window (e.g 'Document 3'), control goes back
to 'Document 2' (and all print messages are printe there) and
if you close that one too, control goes back to 'Document 1'.  This is
ALMOST the basic functinality I need!
(There appear error messages:
>>> QPainter::begin: Another QPainter is already painting this device; A
paint device can only be painted by one QPainter at a time  ....
at random times, which I don't understand, but in principle my construction
seems to work so far!)



THE PROBLEM (driving me nuts!):
=========================


More information about the PyQt mailing list