[vtkusers] Setup an interactor when using vtkImageViewer

Gheorghe Postelnicu gheorghe.postelnicu at gmail.com
Mon Sep 4 16:07:19 EDT 2006


Hi Mathieu,

Indeed, my hasty previous post was missing some elements. What I want
to achieve is the following:
- consider a image actor displaying orthogonal slices in a volume;
- add to that the possibility to interact with the data in different
manners, based on the mode of the application - in one mode, you would
do exactly what the ImagePlaneWidget allows you to do by default, i.e.
set the contrast and brightness in the image; however, it is mandatory
I am able to change this mode and have my mouse events be interpreted
differently if I want it to - hence, I want to be able to change the
interactor style ( => override the default observers).

In the following code, I am trying to specify an interactor style that
would allow me to implement my own event observers, but this doesn't
seem to work. Any suggestions?

Thanks,

I cannot seem to be able to achieve this using the following code:


import vtk
from vtk.tk.vtkTkImageViewerWidget import \
     vtkTkImageViewerWidget
from vtk.tk.vtkTkRenderWindowInteractor import \
     vtkTkRenderWindowInteractor
import sys
import Tkinter

def LoadVolume():
    if len(sys.argv) < 2:
        print " Error - please provide a file name (.vtk)"
        exit
    fileName = sys.argv[1]

    reader = vtk.vtkStructuredPointsReader()
    reader.SetFileName( fileName )
    reader.Update()
    return reader.GetOutput()


volume = LoadVolume()

viewer = vtk.vtkImageViewer()
viewer.SetInput(volume)
viewer.SetZSlice(130)
viewer.SetColorWindow(2000)
viewer.SetColorLevel(1000)

root = Tkinter.Tk()
root.withdraw()

top = Tkinter.Toplevel(root)

def quit(obj=root):
    obj.quit()
top.protocol("WM_DELETE_WINDOW",quit)

frmOne = Tkinter.Frame(top)
frmTwo = Tkinter.Frame(top)

for frm in (frmOne,frmTwo):
    frm.pack(padx=3,pady=3,side="top",
                anchor="n",fill="both",
                expand="false")

vtkiw2 = vtkTkImageViewerWidget(frmOne,
                                iv=viewer,
                                width=600,
                                height=600)
vtkiw2.pack()
vtkiw2.Render()

root.update()

def SetSlice(si):
    viewer.SetZSlice(int(si))
    viewer.Render()

sliceIndex = Tkinter.IntVar()
sliceIndex.set(0)
scale = Tkinter.Scale( frmTwo,
                       from_=0,
                       to=100,
                       orient="horizontal",
                       command=SetSlice,variable=sliceIndex,
                       label="Slice")
scale.pack(fill="x",expand="false")

# set special interactor
def Boom(obj,ev):
    print " boom "

interactor = vtkTkRenderWindowInteractor(root)

istyle = vtk.vtkInteractorStyleUser()
istyle.AddObserver("LeftButtonPressEvent", Boom)
interactor.SetInteractorStyle( istyle )

viewer.SetupInteractor(interactor)

renWin = viewer.GetRenderWindow()


root.mainloop()


On 9/4/06, Mathieu Malaterre <mathieu.malaterre at kitware.com> wrote:
> Gheorghe Postelnicu wrote:
> > Hi,
> >
> > I would like to use vtkImageViewer in a Python script, but I have a
> > hard time using the interactor associated with the renderer.
> >
> > The method viewer.GetRenderWindow().GetInteractor() returns None and I
> > see there is a protected member interactor in vtkImageViewer.
>
> Gheorghe,
>
>         You are not describing the steps you followed. Have you read the online
> documentation for vtkImageViewer ?
>
> http://www.vtk.org/doc/nightly/html/classvtkImageViewer.html
>
>         Have look at the tests/examples associated with vtkImageViewer ? Do
> they miss something you are trying to achieve ?
>
> http://www.vtk.org/doc/nightly/html/c2_vtk_e_2.html#c2_vtk_e_vtkImageViewer
> http://www.vtk.org/doc/nightly/html/c2_vtk_t_7.html#c2_vtk_t_vtkImageViewer
>
>         You seems to be using VTK+Tk+Python. Have you played with the examples
> in VTK/Wrapping/vtk/tk. Do they work for you ? What are they missing ?
>
>         If you see a problem, please post a simple code to reproduce. In your
> case, a simple python+tk script that demonstrate the problem is fine
> (use Data from VTKData is possible). Do not forget to specify the
> version of VTK you are using.
>
> Thanks
> Mathieu
>


-- 
Gheorghe Postelnicu, PhD
MGH, Harvard Medical School



More information about the vtkusers mailing list