title: PyQt5 + VTK - wglMakeCurrent failed error
body: <p>Minimum working example:</p>
  <pre><code>from sys import exit
  from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QHBoxLayout, QLabel, QFrame
  from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
  import vtkmodules.all as vtk
  from vtkmodules.util import numpy_support
  import numpy as np
  
  vtk_out = vtk.vtkOutputWindow()
  vtk_out.SetDisplayMode(0)
  
  def generate_qtvtk_sphere(parent):
      frame = QFrame(parent)
      vtkWidget = QVTKRenderWindowInteractor(frame)
  
      ren = vtk.vtkRenderer()
      vtkWidget.GetRenderWindow().AddRenderer(ren)
      iren = vtkWidget.GetRenderWindow().GetInteractor()
  
      # Create source
      source = vtk.vtkSphereSource()
      source.SetCenter(0, 0, 0)
      source.SetRadius(5.0)
  
      # Create a mapper
      mapper = vtk.vtkPolyDataMapper()
      mapper.SetInputConnection(source.GetOutputPort())
  
      # Create an actor
      actor = vtk.vtkActor()
      actor.SetMapper(mapper)
  
      ren.AddActor(actor)
  
      ren.ResetCamera()
  
      iren.Initialize()
      iren.Start()
  
      return vtkWidget
  
  def generate_qtvtk_imagedata(parent):
      frame = QFrame(parent)
      vtkWidget = QVTKRenderWindowInteractor(frame)
  
      random_array = np.random.random([10, 600, 800])
      vtk_image_data = vtk.vtkImageData()
  
      image_array = numpy_support.numpy_to_vtk(random_array.ravel(), deep=True, array_type=vtk.VTK_TYPE_UINT16)
      vtk_image_data.GetPointData().SetScalars(image_array)
      vtk_image_data.Modified()
  
      reslice = vtk.vtkImageReslice()
      reslice.SetInputData(vtk_image_data)    
      reslice.SetOutputDimensionality(2)
      
      ren = vtk.vtkRenderer()
      vtkWidget.GetRenderWindow().AddRenderer(ren)
      iren = vtkWidget.GetRenderWindow().GetInteractor()
  
      # Create an actor
      actor = vtk.vtkImageActor()
      actor.GetMapper().SetInputConnection(reslice.GetOutputPort())
      ren.SetBackground(50, 50, 50)
  
      ren.AddActor(actor)
  
      ren.ResetCamera()
  
      interactorStyle = vtk.vtkInteractorStyleImage()
      vtkWidget.SetInteractorStyle(interactorStyle)
      vtkWidget.GetRenderWindow().SetInteractor(vtkWidget)
      vtkWidget.GetRenderWindow().Render()
  
  
      iren.Initialize()
      iren.Start()
  
      return vtkWidget
  
  
  app = QApplication([])
  main_window = QMainWindow()
  
  main_widget = QWidget()
  main_layout = QHBoxLayout()
  main_widget.setLayout(main_layout)
  
  qtvtk_widget_sphere = generate_qtvtk_sphere(main_widget)
  qtvtk_widget_image = generate_qtvtk_imagedata(main_widget)
  
  main_layout.addWidget(qtvtk_widget_image)
  main_layout.addWidget(qtvtk_widget_sphere)
  
  main_window.setCentralWidget(main_widget)
  
  
  main_window.show()
  app.exec()
  </code></pre>
  <p>The output of this code should be a pyqt window with a sphere on one side and a randomly-generated black square on the other. The square is representative of the datasets that I normally deal with.</p>
  <p>The relevant packages in my conda env are:</p>
  <ul>
  <li>vtk 9.1.0</li>
  <li>pyqt5 5.15.7</li>
  <li>python 3.7.13 (cant upgrade due to dependency on another package)</li>
  </ul>
  <p>The full error I get looks like this:</p>
  <p><a href="https://i.stack.imgur.com/iHeW2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iHeW2.png" alt="enter image description here" /></a></p>
  <hr />
  <p>Original post follows:</p>
  <p>In my application, I have two <code>QVTKRenderWindowInteractor</code>, on two different QWidgets, that are mostly independent from each other - there's a function outside the interactors that updates the contents of both interactors at the same thing, but that's it.</p>
  <p>Whenever the application displays both interactors, I get the following error when the app closes:</p>
  <pre><code>2022-08-23 13:51:07.522 (  10.079s) [                ]vtkWin32OpenGLRenderWin:267    ERR| vtkWin32OpenGLRenderWindow (00000194B174E4C0): wglMakeCurrent failed in MakeCurrent(), error: ࣐Ɣ
  </code></pre>
  <p>The error part in the end looks like this on the debugger</p>
  <p><a href="https://i.stack.imgur.com/5cQSO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5cQSO.png" alt="enter image description here" /></a></p>
  <p>and there's usually a lot of these errors that pop up.</p>
  <p>My other application (same idea, qt, same interactor, same set-up) has these lines in the beginning of the code</p>
  <pre><code>vtk_out = vtkOutputWindow()
  vtk_out.SetDisplayMode(0)
  </code></pre>
  <p>which suppresses the errors for that one.</p>
  <p>But in my other progra,, I need to use a QWorker/QThread in order to update the interactor, and outputting the error to console doesn't work anymore (it creates a new window with the same error, and then freezes everything).</p>
  <p>I just want to know what the heck these errors are and how to get rid of them.</p>
  <p>Also, the code I'm using the setup the interactor is as follows:</p>
  <p>In the widget:</p>
  <pre><code>        self._frame = QGroupBox()
          self.interactor = ViewerPanelVTK(self._frame)
  
          layout = QVBoxLayout(self)
          layout.addWidget(self.interactor)
  </code></pre>
  <p>where</p>
  <pre><code>class ViewerPanelVTK(QVTKRenderWindowInteractor):
      def __init__(self, parent=None):
          super().__init__(parent)
          self.panel_actor = vtkImageActor()
          self.panel_renderer = vtkRenderer()
          self.window = self.GetRenderWindow()
  </code></pre>