[Paraview] Python scripts with PyQt4
pat marion
pat.marion at kitware.com
Wed Apr 21 18:20:32 EDT 2010
PyQt can be made to work with paraview. Make sure paraview and pyqt use the
same version of qt. It works for me using qt 4.6.2 compiled from source,
and system pyqt (apt-get install python-qt4.) Don't create a QApplication
or call exec_(), paraview already has an application event loop running. A
minimal example would look like:
from PyQt4 import QtGui
b = QtGui.QPushButton("click me")
b.show()
But... due to the way paraview embeds python you can't use pyqt signal/slot
callbacks without some hackery. Paraview creates multiple python
interpreter contexts, so callbacks don't automatically execute under the
correct context. The issue is described here:
http://www.vtk.org/pipermail/vtk-developers/2009-June/006091.html
You can work around the issue by wrapping python functions that are called
via qt signals with @callback_wrapper. Here is a minimal example that shows
a combo box, changing selection in the combo box creates objects in
paraview:
def callback_wrapper(func):
"""A decorator that wraps a python function with a vtk callback"""
v = paraview.vtk.vtkObject()
def wrap(*args, **kwargs):
v._args = args
v._kwargs = kwargs
v.Modified()
def callback(o, e): func(*v._args, **v._kwargs)
v.AddObserver("ModifiedEvent", callback)
return wrap
@callback_wrapper
def onComboChanged(value):
print "You selected:", value
if value == "Sphere": Sphere()
if value == "Cone": Cone()
if value == "Cylinder": Cylinder()
from PyQt4 import QtCore, QtGui
c = QtGui.QComboBox()
for s in ["Sphere", "Cone", "Cylinder"]: c.addItem(s)
c.connect(c, QtCore.SIGNAL('currentIndexChanged(const QString&)'),
onComboChanged)
c.show()
One final disclaimer: if errors occur while executing pyqt code, paraview
can hang instead of reporting the error message and returning...
Pat
On Sat, Apr 17, 2010 at 1:39 PM, Berk Geveci <berk.geveci at kitware.com>wrote:
> Hi Stefan,
>
> I am afraid what you are trying to do is not supported. I am saying
> "not supported" rather than "impossible" because someone smarter than
> me may be able to figure it out. We never envisioned it as a use case.
> I think you have two choices: use C++ to extend ParaView or use PyQt
> from python/pvpython building the GUI from scratch. If you are
> interested in the second case, I have some code that shows how to
> embed the render window(s) into PyQt widgets.
>
> -berk
>
> On Fri, Apr 9, 2010 at 2:39 PM, Stefan Kroboth <ml at stefan-kroboth.com>
> wrote:
> > Hello!
> >
> > I've build a Gui in the QtDesigner and converted it into a *.py file
> using pyuic4. Then I wrote a small program which loads and shows this
> Window. This program works fine in Python 2.6 but fails in the ParaView
> python shell. Since I'm new to Qt I don't know where to look for help. Is it
> even possible to display custom Windows?
> > This is the code I tried (based on a tutorial):
> >
> > import sys
> > from paraview.simple import *
> > from PyQt4 import QtCore, QtGui
> > from MITgui import Ui_MIT_Automator
> >
> > class Qt_MIT_Gui(QtGui.QMainWindow):
> > def __init__(self, parent=None):
> > QtGui.QWidget.__init__(self, parent)
> > self.ui = Ui_MIT_Automator()
> > self.ui.setupUi(self)
> >
> >
> > if __name__ == "__main__":
> > bla = QtGui.QApplication(sys.argv)
> > test = Qt_MIT_Gui()
> > test.show()
> > sys.exit(test.exec_())
> >
> > 1. The first problem is sys.argv. I solve that by assigning ["path to the
> script"] to it.
> > 2. When 1. is solved, it gets stuck in the line "bla =
> QtGui.QApplication(sys.argv)". When I comment that line, it ...
> > 3. ... gets to "QtGui.QWidget.__init__(self, parent)", where it gets
> stuck again. ParaView just doesn't do anything and has to be killed.
> >
> > I tried a lot of things, which were mostly just random alterations
> (because of my lack of knowledge).
> > It would be very nice if someone could provide me a minimal example of
> how to load and display a Qt gui (if this is even possible).
> >
> > Please excuse once again my bad englisch :)
> >
> > Thanks,
> > Stefan
> > _______________________________________________
> > Powered by www.kitware.com
> >
> > Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
> >
> > Please keep messages on-topic and check the ParaView Wiki at:
> http://paraview.org/Wiki/ParaView
> >
> > Follow this link to subscribe/unsubscribe:
> > http://www.paraview.org/mailman/listinfo/paraview
> >
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the ParaView Wiki at:
> http://paraview.org/Wiki/ParaView
>
> Follow this link to subscribe/unsubscribe:
> http://www.paraview.org/mailman/listinfo/paraview
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.paraview.org/pipermail/paraview/attachments/20100421/bda202b3/attachment.htm>
More information about the ParaView
mailing list