This is an archival dump of old wiki content --- see scipy.org for current material.
Please see http://scipy-cookbook.readthedocs.org/

Example code for embedding Matplotlib in Qt applications is given at embedding_in_qt.py. This recipe extends that basic formula for integration with other powerful tools. In particular, we bring together the use of the GUI creation tool made by Trolltech (the creators of Qt) and the ability to interact with a running Qt application via IPython.

A basic tutorial for using Designer (along with the pertinent system requirements) has been posted by Alex Fedosov. Please review it before continuing.

However, for our purposes, we will create a much simpler design in Designer.

Open Designer, and create a new "Main Window":

designer_newopen.png

When the wizard appears, remove all of the menus and toolbars that it suggests to generate:

designer_wizard.png

Now add a custom widget to your project as:

designer_edit_custom_widgets.png

Give this new widget the name of MatplotlibWidget and set the size to 400, 300:

designer_new_widget.png

The MatplotlibWidget should now appear in your toolbox. Click on it and then click in the Form that has been created. Also perform "Lay Out Vertically (Ctrl+L)" and "Adjust Size (Ctrl+J)" operations on the Form (without the widget being selected) if you know what those are. At this point your workspace might look something like this:

designer_full_workspace.png

Now we need to get the "import" setting put in, as specified here:

designer_form_settings_comment.png

Of course, you'll also need the file that will then be included: mplwidget.py.

The product of all of these operations in Designer is a .ui file. So, save the Form we've been working on and call it "mplwidget_tutorial.ui".

One of the things that can mess this up is that Designer automatically increments the names given to widget instances (Form1, Form2, matplotlibWidget1, matplotlibWidget2, etc.) so if those don't match up with what I used, you may need to make some logical adjustments to your procedure.

Now, we use the tool pyuic (included with PyQt to convert this .ui file into a Python class. This is easily accomplished with:

pyuic mplwidget_tutorial.ui > mplwidget_tutorial.py

Go ahead and view the contents of mplwidget_tutorial.py and compare it with what I got. You can also look at my mplwidget_tutorial.ui

Now, it's nice to have the invocation written up in its own main_mpl_tutorial.py file, which is amazingly short:

   1 from mplwidget_tutorial import *
   2 
   3 f = Form1()
   4 f.show()

Then, we want to start ipython and instantiate the window. In order to succeed in this endeavor, there are a bunch of packages (each with minimum version requirements) you'll need, like python2.3-ipython, python2.3-qt3, and so on. The most important one, though, is ipython >= 0.6.13 (I think). From that version on, there is a super-great feature that adds an invocation switch -qthread that starts a QApplication in a separate thread, in such a way that the ipython prompt can still interact with it.

ipython_invoked.png

So, if the above command doesn't work for you, check the version. It may also be invoked as "python2.3-ipython" depending on your configuration.

Okay, now comes the coolest part: the interaction.

ipython_interacted.png

And, it is very easy to go back to Designer, add a button, re-run pyuic, and you've got another version.


CategoryCookbookMatplotlib

SciPy: Cookbook/Matplotlib/Qt_with_IPython_and_Designer (last edited 2015-10-24 17:48:26 by anonymous)