1.背景
vtk版本:9.2.6
py版本:3.9
2.遇到的问题
class ModelInteractorStyle(vtkInteractorStyleTrackballActor):
def __init__(self):
super().__init__()
self.Actor = None
def OnLeftButtonDown(self):
print("左键点击")
vtkInteractorStyleTrackballActor.OnLeftButtonDown(self)
def OnMiddleButtonDown(self):
print("Middle button down")
vtkInteractorStyleTrackballActor.OnMiddleButtonDown(self)
第一像这样写,类似于C++写法,想要重写虚函数,发现点击鼠标之后,没有输出print的内容,不清楚为什么?难道不能像C++那样重写?(求助万能网友)
3.解决办法
class CurveInteractorStyle(vtkInteractorStyleTrackballCamera):
def __init__(self, parent=None):
self.AddObserver('LeftButtonPressEvent', self.left_button_press_event)
self.AddObserver('RightButtonPressEvent', self.right_button_press_event)
def OnLeftButtonDown(self):
vtkInteractorStyleTrackballCamera.OnLeftButtonDown(self) # 同时保持左击正常功能
print("左键点击")
def OnRightButtonUp(self):
vtkInteractorStyleTrackballCamera.OnRightButtonUp(self)
print("右键点击")
def left_button_press_event(self, obj, event):
print('Left mouse press')
self.OnLeftButtonDown()
return
def right_button_press_event(self, obj, event):
print('Right mouse press')
self.OnRightButtonUp()
return
第二次这样写即可,我自己写一个事件,加入一个观察者,然后把鼠标交互的函数放入到事件函数当中,就能够检测到鼠标的点击事件了。亲测成功!