Actually, if only one widget was added in the demo codes, no error appears after closing the application window. But when two widgets (w1 and w2) were added, there are errors when closing.
Here is the modified code:
from vtk import *
from vtkmodules.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from PyQt5 import QtWidgets
class ChartWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
layout = QtWidgets.QVBoxLayout()
self.setLayout(layout)
self.widget = QVTKRenderWindowInteractor(self)
layout.addWidget(self.widget)
chart = vtkChartXY()
self.view = vtkContextView()
self.view.SetRenderWindow(self.widget.GetRenderWindow())
self.view.GetScene().AddItem(chart)
# x-y plot
f = vtkPiecewiseFunction()
f.AddPoint(0, 0.0)
f.AddPoint(500, 0.5)
pf = vtkPiecewiseFunctionItem()
pf.SetPiecewiseFunction(f)
chart.AddPlot(pf)
self.view.GetInteractor().Initialize()
self.view.GetInteractor().Start()
class MainWindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()
layout = QtWidgets.QHBoxLayout()
self.setLayout(layout)
self.w1 = ChartWidget()
layout.addWidget(self.w1)
self.w2 = ChartWidget() # no error if these two lines were commented out.
layout.addWidget(self.w2) # no error if these two lines were commented out.
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()