在PySide中嵌入VTK

在用C++时,作为官方搭配,Qt和VTK是一对好基友,本文试试python下这对基友是否暧昧依旧? 这里用的是Qt的python版本:PySide,下载官方编译好的python支持包:vtkpython-6.3.0-Windows-64bit.exe。

在PySide中嵌入VTK_第1张图片
0.环境变量:
Path : D:\Program Files\VTK 6.3.0\bin;
PYTHONPATH : D:\Program Files\VTK 6.3.0\bin\Lib\site-packages;

1. 导入vtk包:
import vtk
from vtk import *
测试VTK是否可用:
print vtk.vtkVersion.GetVTKSourceVersion()

2. 测试代码:
import vtk    

cone_a=vtk.vtkConeSource()    

coneMapper = vtk.vtkPolyDataMapper()  
coneMapper.SetInputConnection(cone_a.GetOutputPort())  
  
coneActor = vtk.vtkActor()  
coneActor.SetMapper(coneMapper)    
  
ren1= vtk.vtkRenderer()  
ren1.AddActor( coneActor )  
ren1.SetBackground( 0.1, 0.2, 0.4 )  
  
renWin = vtk.vtkRenderWindow()  
renWin.AddRenderer( ren1 )  
renWin.SetSize( 300, 300 )  
renWin.Render()  
  
iren=vtk.vtkRenderWindowInteractor()  
iren.SetRenderWindow(renWin)  
  
iren.Initialize()  
iren.Start()
3. GUI,只贴出main.py,自动生成的ui.py就不贴了。这样写挺烦,不过之后应该能封装。嗯,先这样,一步一步来。
__author__ = 'winston-pc'

from PySide import QtCore, QtGui
from PySide.QtGui import QApplication

import vtk
from vtk.qt4.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
import sys

class MWindow(QMainWindow,Ui_MainWindow):
    def __init__(self, parent=None):
        super(MWindow,self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.setupUi(self)

        self.ui.gridlayout = QtGui.QGridLayout(self.widget)
        self.ui.vtkWidget = QVTKRenderWindowInteractor(self.widget)
        self.ui.gridlayout.addWidget(self.ui.vtkWidget, 0, 0, 1, 1)

        self.ren = vtk.vtkRenderer()
        self.ui.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
        self.iren = self.ui.vtkWidget.GetRenderWindow().GetInteractor()

......
# Create source
source = vtk.vtkConeSource()

# Create a mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(source.GetOutputPort())

# Create an actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)

self.ren.AddActor(actor)
self.ren.SetBackground( 0.1, 0.2, 0.4 )
......

# Main Function
if __name__=='__main__':
    app = QApplication(sys.argv)
    Window = MWindow()
    Window.show()
    Window.iren.Initialize()
......
    sys.exit(app.exec_())
    #Program.exec_()
在PySide中嵌入VTK_第2张图片

总是想做的太多,能做的太少,时间和精力只有那么多,所以整个故事的结果就又交给了选择,哎~~!
关键词:自律
在PySide中嵌入VTK_第3张图片
参考文献:
1. http://www.cnblogs.com/21207-iHome/articles/6427253.html
2. http://www.vtk.org/Wiki/VTK/Examples/Python/Widgets/EmbedPyQt2


你可能感兴趣的:(#,python)