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

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