使用pyqt(在Ubuntu 18.04上为Python3.7),我创建了一个GUI,在其中渲染了一些我想创建视频的VTK内容。因此,我需要在主窗口Qwidget.grab()并将其保存到png文件中(以便将图像组合为视频文件)。 这对窗口上的所有内容(包括滑块,组合框等QWidget和matplotlib图像)均适用,但渲染的VTK对象(QVTKRenderWindowInteractor)除外,因为它将图片中的对象替换为黑框或有时出现白噪声。
我给出了一个最小的问题示例,希望主要问题在于整个程序的复杂性。不幸的是,即使只用最少的代码,它仍然不会呈现窗口的VTK部分。
这是我的问题的一个最小示例,其中单击“打印”按钮应获取图像并将其保存到PNG文件。
import sys
from PyQt5.QtWidgets import QVBoxLayout,QPushButton, QWidget, QApplication
import vtk
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
def print_screen_callback(w):
w.grab().save("non_satisfying_printed_window.png")
# Create a small interface that show a cylinder and allow to print it to a PNG file
app = QApplication(sys.argv)
w = QWidget()
w.show()
lay = QVBoxLayout()
w.setLayout(lay)
# Create and populate the vtk widget
ren = vtk.vtkRenderer()
w_vtk = QVTKRenderWindowInteractor()
lay.addWidget(w_vtk)
w_vtk.Initialize()
w_vtk.Start()
w_vtk.GetRenderWindow().AddRenderer(ren)
cylinder = vtk.vtkCylinderSource()
cylinder.SetResolution(8)
cylinderMapper = vtk.vtkPolyDataMapper()
cylinderMapper.SetInputConnection(cylinder.GetOutputPort())
cylinderActor = vtk.vtkActor()
cylinderActor.SetMapper(cylinderMapper)
ren.AddActor(cylinderActor)
# Add print screen button
but = QPushButton("Print")
lay.addWidget(but)
but.released.connect(lambda: print_screen_callback(w))
# Run the application
app.exec()
当人按下“打印”按钮时,预期的输出当然是实际的窗口。