[PyKDE] How to do *INTERACTIVE* plotting with PyQt (or PyQwt)
phil at river-bank.demon.co.uk
Thu Sep 5 09:18:00 BST 2002
Peter Lipa wrote:
> First, let me thank you for providing and maintaining the PyQt package!
> As a feedback, we compiled and installed sucessfully your package and demos
> for the commercial Qt3.0.4 and Qt3.0.5 versions on Win2K (the latest
> PyQt 3.3 and sip3.3.2 versions!).
> Now my problem, which I think is a very fundamental one, that needs to
> be resolved to make a PyQt based, interactive
> plotting package:
> I am banging my head against the wall in trying to get PyQt (or PyQwt)
> to allow for INTERACTIVE plotting
> such as with the scipy.gplt (using gnuplot) or scipy.plt (using
> wxWindows) packages. I want a similar API as matlab plotting commands or
> the scipy.plt API but based on PyQt (and/or PyQwt but the problem is at
> the PyQt eventloop and threading level).
> All examples with PyQt (and PyQwt demos etc) assume that one has only
> ONE pyqt app running and, if called as a script, block the python
> interpreter until the PyQt window is closed -- or one runs each demo as
> a stand alone app.
> In order to do INTERACTIVE plotting one needs to issue plot commands
> from the python interpeter command line such as:
> >>> import qplt
> >>> x = [0,1,2,3, 4]; y = [0,1.5,3,1.5,0]
> >>> fig1 = qplt.plot(x,y) # creates a figure window with default axes
> and line plot with x versus y and returns control to the interpreter
> >>> qplt.xlabel("x-axis") # adds a x-label to the current axes in the
> current figure
> >>> qplt.ylabel("y-axis") # adds a y-label to the current axes in the
> current figure
> >>> fig2 = qplt.figure() # creates a second figure window and all
> subsequent commands (plot(), xlabel() etc.) apply to this new window
> etc, etc
> In order to get such functionality one needs to create a qt qApp
> singleton, a figure main-window, start the eventloop in a separate
> thread (either python thread or QThread) and gives control back to the
> interpeter to let the user issue new commands (e.g. another figure
> main-window) interactively.
> This seems to be a simple and natural approach to interactive plotting,
> but I have not found any example or idea based on pyQt that would give
> me a hint how to prevent the Qt eventloop qApp.exec_loop() blocking the
> interpreter or how to add new QMainWindows from the interpreter after
> the eventloop is already running in a separate python thread.
> So far I have managed to to open a pyqt figure-window from the
> interpreter by wrapping the main() with the eventloop qApp.exec_loop()
> into a python thread; but then I can't open another window from the main
> thread. Using the QThread class didn't help at all: the subclassed
> myQThread.run() does never give control back to the interpeter if it
> contains the qApp.exec_loop(), and myQThread.start() does never call
> myQThread.run() but the dummy QThread.run(). It is not clear from the
> Qt (and PyQt) docu if it is possible to run the main Qt eventloop in a
> QThread; in my experiments it always blocked
> any GUI.
> I have tried and tried (using all possible varaints of python threads as
> well as QThread ) and can't figure out how to do it.
> Do you have any idea how to do real *INTERACTIVE* plotting with PyQt?
> Essentially all that needs to be done is to start a qApp and qt
> eventloop in a separate thread and launch several figure windows and
> from the python interpreter thread.
> Since this is a fundamental problem that many people in the scientific
> area need resolved I hope that someone on this list (or maybe someone
> you know) knows alread how to do this!
> Any pointers, ideas or sample code would be GREAT!
eric is able to do pretty much what you describe. It presents you with a
Python interpreter that you can interactively enter Python statements in
while the same interpreter is running a PyQt application. There may be
easier ways of doing it though.
The problem you describe with QThread sounds like you weren't using it
properly - although I couldn't say if running the event loop from a
thread is expected to work. (Might do, so long as you manage the Qt lock
More information about the PyQt