# ExitCallback() closes the window

**URL:** <https://discourse.vtk.org/t/exitcallback-closes-the-window/10949>\
**Category:** Development\
**Created:** [March 12, 2023, 4:34pm UTC](https://discourse.vtk.org/t/exitcallback-closes-the-window/10949 "2023-03-12T16:34:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcomusy](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marcomusy/32/95_2.png) [@marcomusy](https://discourse.vtk.org/u/marcomusy)\
**Post date:** [March 12, 2023, 4:34pm UTC](https://discourse.vtk.org/t/exitcallback-closes-the-window/10949/1 "2023-03-12T16:34:08Z")

</div>

I notice this unexpected behavior, calling `iren.ExitCallback()` closes the rendering window which pops up again at the next `iren.Start()`:

```python
import time
import vtk

cone = vtk.vtkConeSource()
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(cone.GetOutputPort())

actor = vtk.vtkActor()
actor.SetMapper(mapper)

ren = vtk.vtkRenderer()
ren.AddActor(actor)

renWin = vtk.vtkRenderWindow()
renWin.SetSize(500, 500)
renWin.AddRenderer(ren)
renWin.Render()

iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

def callb(iren, ename):
    print("calling ExitCallback")
    iren.ExitCallback()

iren.RemoveAllObservers()
iren.AddObserver('KeyPressEvent', callb)

# iren.Initialize() # needed?
iren.Start()

print("window should stay open, but it closes")
time.sleep(1)
iren.Start() # pops up again

```

system info:

```plaintext
vtk version : 9.2.6
numpy version : 1.24.2
python version : 3.10.9 (main, Jan 11 2023, 15:21:40) [GCC 11.2.0]
python interpreter: /home/musy/soft/miniconda3/bin/python
system : Linux 5.4.0-144-generic posix x86_64 (Ubuntu 20.04.5 LTS)

```

---

<div class="post-metadata">

**Author:** ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)\
**Post date:** [March 13, 2023, 10:42pm UTC](https://discourse.vtk.org/t/exitcallback-closes-the-window/10949/2 "2023-03-13T22:42:42Z")

</div>

Why are you removing all the observers, namely `iren.RemoveAllObservers()`?

I don’t see the window closing at all in Windows. As a tesi, I commented out `iren.RemoveAllObservers()` so that I could interact with the image using the mouse, and, when a key is pressed, the timer is invoked and you cannot interact with the image. Note that in this case keypress events are passed through so pressing “w” will display a wireframe before the timer is invoked. After the timer expires you can interact with the image. If you press a key again, the script exits.  
**Note:** I increased your timer interval to 10 to demonstrate this behaviour.

---

<div class="post-metadata">

**Author:** ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)\
**Post date:** [March 14, 2023, 1:29am UTC](https://discourse.vtk.org/t/exitcallback-closes-the-window/10949/3 "2023-03-14T01:29:34Z")

</div>

The closing of the window in `TerminatApp()` seems to be specific to X11.

The underlying X Window seems to be destroyed and replaced with a new one:

```plaintext
Before: After:
vtkXOpenGLRenderWindow (0x565458130300) vtkXOpenGLRenderWindow (0x565458130300)
  ContextId: 0x565458152220 ContextId: 0x56545862f100
  Display Id: 0x55df23fc04c0 Display Id: 0x565458838b70
  Window Id: 85983234 Window Id: 88080386

```

Someone would have to trace their way through the code to see why `TerminateApp()` doesn’t just exit the loop.

Edit: It looks like the `Finalize()` call to close the window was added to `TerminateApp()` in VTK 9.1, and it has moved around a bit since then.
