Close VTK window (Python)

Consider the following script

import vtk

ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

an_actor = vtk....  # create an actor
ren.AddActor(an_actor)

iren.Initialize()
renWin.Render()
iren.Start()

If the script ends, everything will be fine and the created window will be closed, and resources will be freed when the window is manually closed (click X) or press the exit key (Q or E).

However, if there are more statements, you will notice that the window still exists, which is understandable, because we did not call anything to refuse it, just ended the interaction cycle.

See for yourself by adding the following:

temp = raw_input('The window did not close, right? (press Enter)')

According to VTK / Examples / Cxx / Visualization / CloseWindow , this

iren.GetRenderWindow().Finalize()  # equivalent: renWin.Finalize()
iren.TerminateApp()

must do the job, but it’s not.

What else do I need to do to close a programmatically open window?

+5
source share
1 answer

Short answer

Only one line is missing!

del renWin, iren

def close_window(iren):
    render_window = iren.GetRenderWindow()
    render_window.Finalize()
    iren.TerminateApp()
    del render_window, iren

( script ):

...
iren.Initialize()
renWin.Render()
iren.Start()

close_window(iren)

. ,

del x x.__del__() - x , , x (__del__ ).

(__del__() (AttributeError) iren (vtkRenderWindowInteractor) renWin (vtkRenderWindow))

, iren ( renWin ) script, , () .

( ):

def close_window(iren):
    render_window = iren.GetRenderWindow()
    render_window.Finalize()
    iren.TerminateApp()

:

...
iren.Initialize()
renWin.Render()
iren.Start()

close_window(iren)
del renWin, iren
+9

All Articles