# Integrating VTK with PyQT

**URL:** https://discourse.vtk.org/t/integrating-vtk-with-pyqt/15466
**Category:** Support
**Created:** [April 2, 2025, 9:17am UTC](https://discourse.vtk.org/t/integrating-vtk-with-pyqt/15466 "2025-04-02T09:17:30Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![safa\_harzali1](https://discourse.vtk.org/user_avatar/discourse.vtk.org/safa_harzali1/32/9814_2.png) [@safa\_harzali1](https://discourse.vtk.org/u/safa_harzali1)
#### Post date: [April 2, 2025, 9:17am UTC](https://discourse.vtk.org/t/integrating-vtk-with-pyqt/15466/1 "2025-04-02T09:17:30Z")

</div>

Hi. I am using vtk in my pyQt project.  
The issue I’m encountering is puzzling because the error doesn’t occur every time. Do you have any ideas on what might be causing this and how to diagnose it?

Here is the relevant code snippet of the creation of my viewer:

```auto
class myViewer(QVTKRenderWindowInteractor):
        def __init__ (self, parent=None):
        super(). __init__ (parent) 
        self.Init_view()

    def Init_view(self):
        self.Initialize()
        self.Start()
        self.renderer = vtkRenderer()
        self.GetRenderWindow().AddRenderer(self.renderer)

        self.interactor = self.GetRenderWindow().GetInteractor()
        self.interactor.SetInteractorStyle(self.style_2d) 
                 
        self.renderer.ResetCamera()
        self.show()

```

The following is the results in the terminal:

```auto
  File "C:\Users\.conda\envs\vtkenv\lib\site-packages\vtkmodules\qt\QVTKRenderWindowInteractor.py", line 473, in 
resizeEvent
    self._Iren.SetSize(w, h)
  File "C:\Users\.conda\envs\vtkenv\lib\site-packages\vtkmodules\qt\QVTKRenderWindowInteractor.py", line 416, in 
__getattr__
    elif hasattr(self._Iren, attr):
  File "C:\Users\.conda\envs\vtkenv\lib\site-packages\vtkmodules\qt\QVTKRenderWindowInteractor.py", line 416, in 
__getattr__
    elif hasattr(self._Iren, attr):
  File "C:\Users\.conda\envs\vtkenv\lib\site-packages\vtkmodules\qt\QVTKRenderWindowInteractor.py", line 416, in 
__getattr__
    elif hasattr(self._Iren, attr):
  [Previous line repeated 987 more times]
  File "C:\Users\.conda\envs\vtkenv\lib\site-packages\vtkmodules\qt\QVTKRenderWindowInteractor.py", line 414, in 
__getattr__
    if attr == ' __vtk__':
RecursionError: maximum recursion depth exceeded in comparison

```

Thanks in advance for your help! 😊

---

<div class="post-metadata">

### Author: ![rdbrn](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rdbrn/32/5627_2.png) [@rdbrn](https://discourse.vtk.org/u/rdbrn)
#### Post date: [April 4, 2025, 11:48am UTC](https://discourse.vtk.org/t/integrating-vtk-with-pyqt/15466/2 "2025-04-04T11:48:42Z")

</div>

Maybe do Start() after show()  
of skip Start() alltogether and start the interactor instead.

In my code I do the following:

```python
    viewer.interactor.Initialize()

    widget.show()
    viewer.interactor.Start()

```

---

<div class="post-metadata">

### Author: ![safa\_harzali1](https://discourse.vtk.org/user_avatar/discourse.vtk.org/safa_harzali1/32/9814_2.png) [@safa\_harzali1](https://discourse.vtk.org/u/safa_harzali1)
#### Post date: [April 5, 2025, 8:16am UTC](https://discourse.vtk.org/t/integrating-vtk-with-pyqt/15466/3 "2025-04-05T08:16:13Z")

</div>

Thank you @rdbrn for your response. Could you please provide a full example?

---

<div class="post-metadata">

### Author: ![jadh4v](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jadh4v/32/4149_2.png) [@jadh4v](https://discourse.vtk.org/u/jadh4v)
#### Post date: [April 7, 2025, 7:32pm UTC](https://discourse.vtk.org/t/integrating-vtk-with-pyqt/15466/4 "2025-04-07T19:32:10Z")

</div>

You should make calls in the following order after everything else in the pipeline is already set up:

```auto
window.show()
interactor.initialize()
interactor.start()

```

See the example provided here:

> <https://github.com/Kitware/VTK/blob/master/Wrapping/Python/vtkmodules/qt/QVTKRenderWindowInteractor.py#L633-L680>

---

<div class="post-metadata">

### Author: ![safa\_harzali1](https://discourse.vtk.org/user_avatar/discourse.vtk.org/safa_harzali1/32/9814_2.png) [@safa\_harzali1](https://discourse.vtk.org/u/safa_harzali1)
#### Post date: [April 16, 2025, 4:25pm UTC](https://discourse.vtk.org/t/integrating-vtk-with-pyqt/15466/5 "2025-04-16T16:25:18Z")

</div>

Unfortunately, the problem still persists.

---

<div class="post-metadata">

### Author: ![rdbrn](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rdbrn/32/5627_2.png) [@rdbrn](https://discourse.vtk.org/u/rdbrn)
#### Post date: [April 17, 2025, 6:15am UTC](https://discourse.vtk.org/t/integrating-vtk-with-pyqt/15466/6 "2025-04-17T06:15:02Z")

</div>

Does the following example work for you?  
(you may have to replace PySide6 if you are using different python/qt bindings)

```python
from PySide6.QtWidgets import QApplication, QMainWindow

from vtkmodules.vtkFiltersSources import vtkConeSource
from vtkmodules.vtkRenderingCore import vtkActor, vtkPolyDataMapper, vtkRenderer
import vtkmodules.vtkRenderingOpenGL2
import vtkmodules.vtkInteractionStyle
from vtkmodules.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor

# every QT app needs an app
app = QApplication(['QVTKRenderWindowInteractor'])

window = QMainWindow()

# create the widget
widget = QVTKRenderWindowInteractor(window)
window.setCentralWidget(widget)
# if you don't want the 'q' key to exit comment this.
widget.AddObserver("ExitEvent", lambda o, e, a=app: a.quit())

ren = vtkRenderer()
widget.GetRenderWindow().AddRenderer(ren)

cone = vtkConeSource()
cone.SetResolution(8)

coneMapper = vtkPolyDataMapper()
coneMapper.SetInputConnection(cone.GetOutputPort())

coneActor = vtkActor()
coneActor.SetMapper(coneMapper)

ren.AddActor(coneActor)

# show the widget
window.show()

widget.Initialize()
widget.Start()

app.exec()

```

---

<div class="post-metadata">

### Author: ![safa\_harzali1](https://discourse.vtk.org/user_avatar/discourse.vtk.org/safa_harzali1/32/9814_2.png) [@safa\_harzali1](https://discourse.vtk.org/u/safa_harzali1)
#### Post date: [May 2, 2025, 4:16pm UTC](https://discourse.vtk.org/t/integrating-vtk-with-pyqt/15466/7 "2025-05-02T16:16:25Z")

</div>

Hi again!  
I tried to track down the problem and find where the program stops, and the code execution stops right before this instruction inside the function ` __init__ ` in the file “QVTKRenderWindowInteractor.py”

```auto
WId = self.winId()

```

---

<div class="post-metadata">

### Author: ![rdbrn](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rdbrn/32/5627_2.png) [@rdbrn](https://discourse.vtk.org/u/rdbrn)
#### Post date: [May 9, 2025, 12:15pm UTC](https://discourse.vtk.org/t/integrating-vtk-with-pyqt/15466/8 "2025-05-09T12:15:00Z")

</div>

Are you able to run the example that I gave above?

 ![{E77E15FE-C086-4732-8161-4DB4CC691F35}](https://discourse.vtk.org/uploads/default/original/2X/0/08869a72b11a757d7a086dec229431bc2962c4f4.png)

---

<div class="post-metadata">

### Author: ![safa\_harzali1](https://discourse.vtk.org/user_avatar/discourse.vtk.org/safa_harzali1/32/9814_2.png) [@safa\_harzali1](https://discourse.vtk.org/u/safa_harzali1)
#### Post date: [May 9, 2025, 4:42pm UTC](https://discourse.vtk.org/t/integrating-vtk-with-pyqt/15466/9 "2025-05-09T16:42:36Z")

</div>

Yes, the example worked
