Skip to content

Instantly share code, notes, and snippets.

@samueljohn
Forked from fmorency/gist:2596951
Created July 11, 2012 12:35
Show Gist options
  • Save samueljohn/3090097 to your computer and use it in GitHub Desktop.
Save samueljohn/3090097 to your computer and use it in GitHub Desktop.
VTK Qt/PySide Python example
"""
A simple example that uses the QVTKRenderWindowInteractor
class.
"""
try:
from PySide import QtCore, QtGui
except ImportError:
try:
from PyQt4 import QtCore, QtGui
except ImportError as err:
raise ImportError("Cannot load either PyQt or PySide")
if __name__ == '__main__':
# every QT app needs an app
app = QtGui.QApplication(['QVTKRenderWindowInteractor'])
# create the widget
widget = QVTKRenderWindowInteractor()
widget.Initialize()
widget.Start()
# if you dont want the 'q' key to exit comment this.
widget.AddObserver("ExitEvent", lambda o, e, a=app: a.quit())
ren = vtk.vtkRenderer()
widget.GetRenderWindow().AddRenderer(ren)
cone = vtk.vtkConeSource()
cone.SetResolution(8)
coneMapper = vtk.vtkPolyDataMapper()
coneMapper.SetInputConnection(cone.GetOutputPort())
coneActor = vtk.vtkActor()
coneActor.SetMapper(coneMapper)
ren.AddActor(coneActor)
widget.SetPicker(vtk.vtkPointPicker())
# show the widget
widget.show()
# start event processing
sys.exit(app.exec_())
@samueljohn
Copy link
Author

Small test wheter vtk rendering from python works with PySide or PyQt (if PySide is not found).

@sugeerth
Copy link

PySide version does not work

python2.7/site-packages/vtk/qt/QVTKRenderWindowInteractor.py", line 204, in init
QWidget.init(self, parent, wflags|Qt.MSWindowsOwnDC)
TypeError: QWidget(QWidget parent=None, Qt.WindowFlags flags=0): argument 1 has unexpected type 'PySide.QtGui.QFrame'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment