我在python中开始使用vtk(6)并且在处理KeyPressEvent时遇到问题。我想继承vtkInteractorStyleTrackballCamera
如果我使用这种模式,我的交互器样式没有getKeySym(),我无法解码按下的键
null
但是,如果我使用工厂vtkInteractorStyleTrackballCamera类并添加具有此模式的观察者,则相同的keyPress()可以访问GetKeySym()。
class KeyPressInteractorStyle(vtk.vtkInteractorStyleTrackballCamera):
def __init__(self,parent=None):
self.AddObserver("MiddleButtonPressEvent",self.middleButtonPressEvent)
self.AddObserver("MiddleButtonReleaseEvent",self.middleButtonReleaseEvent)
self.AddObserver("KeyPressEvent",self.keyPress)
def keyPress(self,obj,event):
key = obj.GetKeySym() #Does not work
print("key %s" % key)
return
def middleButtonPressEvent(self,obj,event):
...
return
def middleButtonReleaseEvent(self,obj,event):
...
return
我是新手,我该怎样做才能在课堂上获得功能?
答案 0 :(得分:2)
如果您正在制作一个Interactor Style类,我认为可以安全地假设它将作为某些点应用于交互器。我注意到在您的解决方案中,您将父设置为vtk.vtkRenderWindowInteractor()。将父级设置为vtkRenderWindowInteractor的特定实例会更好:
class MyInteractorStyle(vtk.vtkInteractorStyleTrackballCamera):
def __init__(self,parent=None):
self.parent = iren
self.AddObserver("KeyPressEvent",self.keyPressEvent)
def keyPressEvent(self,obj,event):
key = self.parent.GetKeySym()
if key == 'l':
print(key)
return
...
iren = vtk.vtkRenderWindowInteractor()
iren.SetInteractorStyle(MyInteractorStyle())
iren.SetRenderWindow(renWin)
renWin.Render()
iren.Initialize()
iren.Start()
答案 1 :(得分:0)
我检查了vtk doxygen之后发现了一个解决方案,并且看到vtkInteractorStyleTrackballCamera没有继承vtkRenderWindowInteractor我在移植example时假设的angular-video-bg。我决定将我的样式类传递给父类,以便它可以访问RenderWindow GetKeySym()。它可能不是最好的解决方案,但这是如何作为一个FYI:
class KeyPressInteractorStyle(vtk.vtkInteractorStyleTrackballCamera):
def __init__(self,parent=None):
self.parent = vtk.vtkRenderWindowInteractor()
if(parent is not None):
self.parent = parent
self.AddObserver("KeyPressEvent",self.keyPress)
def keyPress(self,obj,event):
key = self.parent.GetKeySym()
...
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetInteractorStyle(KeyPressInteractorStyle(parent = interactor))
答案 2 :(得分:0)
我通过谷歌搜索“ vtk keypressevent python”发现了这个问题,只是想分享我的解决方案,因为不需要子类化vtkInteractorStyle
:
def keypress_callback(obj, ev):
key = obj.GetKeySym()
print(key, 'was pressed')
render_interactor = vtk.vtkRenderWindowInteractor()
render_interactor.AddObserver('KeyPressEvent', keypress_callback, 1.0)