wglMakeCurrent failed in MakeCurrent after closed a window with two vtk widget.

I create a window, in which I add two vtk widget. The window is OK. But, if I close the winodw, a bug is reporeted:

...

2021-06-01 20:53:09.231 (   3.482s) [                ]vtkWin32OpenGLRenderWin:217    ERR| vtkWin32OpenGLRenderWindow (000001F8981C0530): wglMakeCurrent failed in MakeCurrent(), error: �������

2021-06-01 20:53:09.275 (   3.526s) [                ]vtkWin32OpenGLRenderWin:217    ERR| vtkWin32OpenGLRenderWindow (000001F8981C0530): wglMakeCurrent failed in MakeCurrent(), error: �������

2021-06-01 20:53:09.315 (   3.565s) [                ]vtkWin32OpenGLRenderWin:217    ERR| vtkWin32OpenGLRenderWindow (000001F8981C0530): wglMakeCurrent failed in MakeCurrent(), error: �������

2021-06-01 20:53:09.353 (   3.603s) [                ]vtkWin32OpenGLRenderWin:217    ERR| vtkWin32OpenGLRenderWindow (000001F8981C0530): wglMakeCurrent failed in MakeCurrent(), error: �������

2021-06-01 20:53:09.388 (   3.639s) [                ]vtkWin32OpenGLRenderWin:217    ERR| vtkWin32OpenGLRenderWindow (000001F8981C0530): wglMakeCurrent failed in MakeCurrent(), error: �������

2021-06-01 20:53:09.428 (   3.678s) [                ]vtkWin32OpenGLRenderWin:217    ERR| vtkWin32OpenGLRenderWindow (000001F8981C0530): wglMakeCurrent failed in MakeCurrent(), error: �������

2021-06-01 20:53:09.467 (   3.717s) [                ]vtkWin32OpenGLRenderWin:90     ERR| vtkWin32OpenGLRenderWindow (000001F8981C0530): wglMakeCurrent failed in Clean(), error: 6

The code to reproduce this bug is:

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
import vtk

class vtkWin(QWidget):

    def __init__(self):
        super().__init__()

        layout = QVBoxLayout()
        self.setLayout(layout)
        self.vtkWidget = QVTKRenderWindowInteractor()
        layout.addWidget(self.vtkWidget)
        render = vtk.vtkRenderer()
        self.vtkWidget.GetRenderWindow().AddRenderer(render)

        cone_a=vtk.vtkConeSource()
        coneMapper = vtk.vtkPolyDataMapper()
        coneMapper.SetInputConnection(cone_a.GetOutputPort())
        coneActor = vtk.vtkActor()
        coneActor.SetMapper(coneMapper)
        render.AddActor(coneActor)
        self.vtkWidget.GetRenderWindow().Render()

class MainWindow(QWidget):

    def __init__(self):
        super().__init__()

        layout = QHBoxLayout()
        self.setLayout(layout)

        w1 = vtkWin()
        w2 = vtkWin()
        layout.addWidget(w1)
        layout.addWidget(w2)

if __name__ == '__main__':

    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    app.exec_()

The environment is:

vtk 9.0.1
PyQt5  5.14.4

Any suggestion is appreciated~~~

Finally, I find the solution: we need to manually close the vtk widget

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
import vtk

class vtkWin(QWidget):

    def __init__(self):
        super().__init__()

        layout = QVBoxLayout()
        self.setLayout(layout)
        self.vtkWidget = QVTKRenderWindowInteractor()
        layout.addWidget(self.vtkWidget)
        render = vtk.vtkRenderer()
        self.vtkWidget.GetRenderWindow().AddRenderer(render)

        cone_a=vtk.vtkConeSource()
        coneMapper = vtk.vtkPolyDataMapper()
        coneMapper.SetInputConnection(cone_a.GetOutputPort())
        coneActor = vtk.vtkActor()
        coneActor.SetMapper(coneMapper)
        render.AddActor(coneActor)
        self.vtkWidget.GetRenderWindow().Render()

    def closeEvent(self, QCloseEvent):
        super().closeEvent(QCloseEvent)
        self.vtkWidget.Finalize()     ############################ important

class MainWindow(QWidget):

    def __init__(self):
        super().__init__()

        layout = QHBoxLayout()
        self.setLayout(layout)

        self.w1 = vtkWin()
        self.w2 = vtkWin()
        layout.addWidget(self.w1)
        layout.addWidget(self.w2)

    def closeEvent(self, QCloseEvent):
        super().closeEvent(QCloseEvent)
        self.w1.close()
        self.w2.close()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    app.exec_()
1 Like