[英]how to implement myself interactor style in vtk python
Currently, I need to start our project by using pyqt and vtk.目前,我需要使用 pyqt 和 vtk 来启动我们的项目。 I need to implement myself interact style.我需要实现自己的交互风格。 I inherit a class from vtk.vtkInteractorStyleTrackballCamera.我从 vtk.vtkInteractorStyleTrackballCamera 继承了一个类。 My code is:我的代码是:
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_())
When the left button click, I hope this code can print "left button down", but it don't.当左键单击时,我希望此代码可以打印“左键按下”,但事实并非如此。 what's the wrong with my code?我的代码有什么问题? Any suggestion is appreciated.任何建议表示赞赏。
Try it:尝试一下:
...
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')
...
Update:更新:
how can I get the coordinate of the clicked point?如何获得点击点的坐标?
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.