当前,我需要使用pyqt和vtk启动我们的项目。我需要实现自己的交互风格。我从vtk.vtkInteractorStyleTrackballCamera继承了一个类。我的代码是:
import vtk, sys
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from PyQt5.QtWidgets import *
class myInteractorStyle(vtk.vtkInteractorStyleTrackballCamera):
def __init__(self):
super(myInteractorStyle, self).__init__()
def OnLeftButtonDown(self):
super(myInteractorStyle, self).OnLeftButtonDown()
print('left button down')
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
self.frame = QFrame()
self.vl = QVBoxLayout()
self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
self.vl.addWidget(self.vtkWidget)
self.ren = vtk.vtkRenderer()
self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
# style = vtk.vtkInteractorStyleTrackballCamera()
style = myInteractorStyle()
self.iren.SetInteractorStyle(style)
# Create source
source = vtk.vtkSphereSource()
source.SetCenter(0, 0, 0)
source.SetRadius(5.0)
# Create a mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(source.GetOutputPort())
mapper.ScalarVisibilityOff()
# Create an actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetColor(1, 1, 1)
actor.GetProperty().ShadingOff()
self.ren.AddActor(actor)
self.ren.ResetCamera()
self.frame.setLayout(self.vl)
self.setCentralWidget(self.frame)
self.show()
self.iren.Initialize()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
当单击左键时,希望此代码可以打印“向下按左键”,但不能打印。我的代码有什么问题?
任何建议表示赞赏。
最佳答案
尝试一下:
...
class myInteractorStyle(vtk.vtkInteractorStyleTrackballCamera):
def __init__(self, parent=None):
self.AddObserver("LeftButtonPressEvent", self.leftButtonPressEvent)
def leftButtonPressEvent(self, obj, event):
self.OnLeftButtonDown()
print('left button down')
...
更新:
如何获得点击点的坐标?
class myInteractorStyle(vtk.vtkInteractorStyleTrackballCamera):
def __init__(self, parent=None):
self.AddObserver("LeftButtonPressEvent", self.leftButtonPressEvent)
def leftButtonPressEvent(self, obj, event):
clickPos = self.GetInteractor().GetEventPosition() # <<<-----<
print(f'left button down: {clickPos}')
self.OnLeftButtonDown()
关于python - 如何在vtk python中实现自己的交互器样式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59457218/