How to Solve vtk error (vtkInteractorStyleSwitchBase & vtkWin32OpenGLRenderWin Error)

python3. 7.10
vtk9. one

Error 1

Core code

from vtkmodules.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor

self.lb = QVTKRenderWindowInteractor(self)
self.gridLayout_G.addWidget(self.lb, 0, 0, 0, 0)
self.lb.GetRenderWindow().GetInteractor().Start()

Complete code

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
import sys

from PyQt5 import QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication

from vtkmodules.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor

from SliceViewPanel import Ui_Widget


class SliceViewPanelWidget(QtWidgets.QWidget, Ui_Widget):
    four_views_path = 'resources/dl_fourviews.png'

    def __init__(self, parent=None, plane_name='', icon_path=''):
        super(SliceViewPanelWidget, self).__init__(parent)
        self.setupUi(self)
        self.lb = None
        self.plane_name = plane_name
        self.icon_path = icon_path
        print(plane_name)
        self.var_init()

    def set_tool_icon(self, four_views):
        """
        Set the label of the Show/Hide button
        :param four_views: Hide True/Show False
        """
        if four_views:
            self.toolButton.setIcon(QIcon(SliceViewPanelWidget.four_views_path))
        else:
            self.toolButton.setIcon(QIcon(self.icon_path))

    def var_init(self):
        """
        Clear variables
        """
        self.lb = QVTKRenderWindowInteractor(self)
        self.gridLayout_G.addWidget(self.lb, 0, 0, 0, 0)
        self.G.setTitle(self.plane_name)
        self.set_tool_icon(False)
        self.lb.GetRenderWindow().GetInteractor().Start()

    def close(self):
        """
        Close
        """
        print('close {}'.format(self.plane_name))
        if self.lb is not None:
            self.lb.Finalize()

    def closeEvent(self, event):
        """
        Close event
        :param event: event
        """
        self.close()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    my_widget = SliceViewPanelWidget(parent=None, plane_name='Transverse', icon_path='resources/dl_axial.png')
    my_widget.show()
    sys.exit(app.exec_())

report errors

Warning: In C:\glr\builds\vtk\vtk-ci-ext\0\Rendering\Core\vtkInteractorStyleSwitchBase.cxx, line 37
vtkInteractorStyleSwitchBase (0000021133F56FF0): Warning: Link to vtkInteractionStyle for default style selection.

Solution:

Method 1

These two sentences
and those two sentences are annotated in order not to make pycharm think these two sentences are useless

# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2

Method 2

In fact, the essence is the same as method 1

# noinspection PyUnresolvedReferences
import vtkmodules.all as vtk

Complete code

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
import sys

from PyQt5 import QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication
# noinspection PyUnresolvedReferences
import vtkmodules.all as vtk
from vtkmodules.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor

from SliceViewPanel import Ui_Widget


class SliceViewPanelWidget(QtWidgets.QWidget, Ui_Widget):
    four_views_path = 'resources/dl_fourviews.png'

    def __init__(self, parent=None, plane_name='', icon_path=''):
        super(SliceViewPanelWidget, self).__init__(parent)
        self.setupUi(self)
        self.lb = None
        self.plane_name = plane_name
        self.icon_path = icon_path
        print(plane_name)
        self.var_init()

    def set_tool_icon(self, four_views):
        """
        Set the label of the Show/Hide button
        :param four_views: Hide True/Show False
        """
        if four_views:
            self.toolButton.setIcon(QIcon(SliceViewPanelWidget.four_views_path))
        else:
            self.toolButton.setIcon(QIcon(self.icon_path))

    def var_init(self):
        """
        Clear variables
        """
        self.lb = QVTKRenderWindowInteractor(self)
        self.gridLayout_G.addWidget(self.lb, 0, 0, 0, 0)
        self.G.setTitle(self.plane_name)
        self.set_tool_icon(False)
        self.lb.GetRenderWindow().GetInteractor().Start()

    def close(self):
        """
        Colose
        """
        print('close {}'.format(self.plane_name))
        if self.lb is not None:
            self.lb.Finalize()

    def closeEvent(self, event):
        """
        Close event
        :param event: event
        """
        self.close()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    my_widget = SliceViewPanelWidget(parent=None, plane_name='Transverse', icon_path='resources/dl_axial.png')
    my_widget.show()
    sys.exit(app.exec_())

Error report 2

vtkWin32OpenGLRenderWin:267    ERR| vtkWin32OpenGLRenderWindow (00000169B85B09C0): wglMakeCurrent failed in MakeCurrent(), error:
vtkWin32OpenGLRenderWin:102    ERR| vtkWin32OpenGLRenderWindow (00000169B85B09C0): wglMakeCurrent failed in Clean()

Solution:

First point
this code is called repeatedly in my code
that is, repeated initialization

    def var_init(self):
        self.lb = QVTKRenderWindowInteractor(self)
        self.gridLayout_G.addWidget(self.lb, 0, 0, 1, 1)
        self.lb.GetRenderWindow().GetInteractor().Start()

In fact, just initialize it once

The second point
You should call finalize at the end of the QVTKRenderWindowInteractor program

# self.lb = QVTKRenderWindowInteractor(self)
self.lb.Finalize()

Read More: