# Set Main Window title to name of opened MRB file

**URL:** <https://discourse.slicer.org/t/set-main-window-title-to-name-of-opened-mrb-file/21304>\
**Category:** Support\
**Tags:** python\
**Created:** [January 1, 2022, 10:29pm UTC](https://discourse.slicer.org/t/set-main-window-title-to-name-of-opened-mrb-file/21304 "2022-01-01T22:29:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Greydon\_Gilmore](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/greydon_gilmore/32/6863_2.png) [@Greydon\_Gilmore](https://discourse.slicer.org/u/Greydon_Gilmore)\
**Post date:** [January 1, 2022, 10:29pm UTC](https://discourse.slicer.org/t/set-main-window-title-to-name-of-opened-mrb-file/21304/1 "2022-01-01T22:29:15Z")

</div>

Hi all,

I am generating scenes containing patient imaging/segmentation data, saving as MRB, and sending to our clinical team. They often have several Slicer instances opened with different MRB files loaded.

Is it possible to set each Slicer instance Main Window title to the name of the opened MRB file?

Thank you!

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [January 2, 2022, 4:29pm UTC](https://discourse.slicer.org/t/set-main-window-title-to-name-of-opened-mrb-file/21304/2 "2022-01-02T16:29:25Z")

</div>

Hi,

Two lines of python code (paste into python console for a test) can set the title of the main window:

```auto
mainWindow = slicer.util.mainWindow()
mainWindow.setWindowTitle("Your title")

```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [January 2, 2022, 4:35pm UTC](https://discourse.slicer.org/t/set-main-window-title-to-name-of-opened-mrb-file/21304/3 "2022-01-02T16:35:13Z")

</div>

You can use `slicer.mrmlScene.GetURL()` to add the mrb file name to the window title.

However, since scene files are always imported (current scene content is not replaced) the mrb file name is not always an accurate description of the scene content. It might be safer to rely on what is shown in Data module or in corner annotations:

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/a/4ab6c94a48649e1b458834b3debf843912832171.jpeg)

---

<div class="post-metadata">

**Author:** ![Greydon\_Gilmore](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/greydon_gilmore/32/6863_2.png) [@Greydon\_Gilmore](https://discourse.slicer.org/u/Greydon_Gilmore)\
**Post date:** [January 2, 2022, 6:19pm UTC](https://discourse.slicer.org/t/set-main-window-title-to-name-of-opened-mrb-file/21304/4 "2022-01-02T18:19:06Z")

</div>

@rbumm thank you for the code snippet. I combined this with Andras’s suggestion in the `.slicerrc.py` file. The outcome is exactly what I was looking for.

```auto
import os

@vtk.calldata_type(vtk.VTK_OBJECT)
def onNodeAdded(caller, event, calldata):
	windowTitle = os.path.basename(slicer.mrmlScene.GetURL())
	mainWindow = slicer.util.mainWindow()
	mainWindow.setWindowTitle(windowTitle)

slicer.mrmlScene.AddObserver(slicer.vtkMRMLScene.NodeAddedEvent, onNodeAdded)

```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [January 3, 2022, 2:32pm UTC](https://discourse.slicer.org/t/set-main-window-title-to-name-of-opened-mrb-file/21304/5 "2022-01-03T14:32:22Z")

</div>

Thanks for sharing your solution. Updating the window title is a very lightweight operation, so it should not matter that you do it whenever a node is added to the scene. If you just want to do something when a scene is loaded (e.g., because it is a more lengthy operation) then instead of `slicer.vtkMRMLScene.NodeAddedEvent` you can observe the `slicer.vtkMRMLScene.EndImportEvent` event instead.

---

<div class="post-metadata">

**Author:** ![Greydon\_Gilmore](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/greydon_gilmore/32/6863_2.png) [@Greydon\_Gilmore](https://discourse.slicer.org/u/Greydon_Gilmore)\
**Post date:** [January 4, 2022, 5:46pm UTC](https://discourse.slicer.org/t/set-main-window-title-to-name-of-opened-mrb-file/21304/7 "2022-01-04T17:46:38Z")

</div>

I’ve made the following correction in the `.slicerrc.py` file, using `slicer.vtkMRMLScene.EndImportEvent` instead:

```auto
import os

@vtk.calldata_type(vtk.VTK_OBJECT)
def onEventImportEnd(caller, event):
	windowTitle = os.path.basename(slicer.mrmlScene.GetURL())
	mainWindow = slicer.util.mainWindow()
	mainWindow.setWindowTitle(windowTitle)

slicer.mrmlScene.AddObserver(slicer.vtkMRMLScene.EndImportEvent, onEventImportEnd)

```

For completeness, how should I handle the scene close event. Currently, the Main Window title remains when the scene is closed. I would like the title to return back to default when the scene is closed. I initially was going to use `slicer.vtkMRMLScene.NodeRemovedEvent` but sense this is not the best option

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [January 4, 2022, 5:59pm UTC](https://discourse.slicer.org/t/set-main-window-title-to-name-of-opened-mrb-file/21304/8 "2022-01-04T17:59:29Z")

</div>

> [@Greydon\_Gilmore](#):
>
> For completeness, how should I handle the scene close event.

You can observe the `slicer.vtkMRMLScene.EndCloseEvent` to get notified about scene close.

---

<div class="post-metadata">

**Author:** ![Greydon\_Gilmore](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/greydon_gilmore/32/6863_2.png) [@Greydon\_Gilmore](https://discourse.slicer.org/u/Greydon_Gilmore)\
**Post date:** [January 4, 2022, 6:16pm UTC](https://discourse.slicer.org/t/set-main-window-title-to-name-of-opened-mrb-file/21304/9 "2022-01-04T18:16:05Z")

</div>

This is the final solution:

```auto
import os

@vtk.calldata_type(vtk.VTK_OBJECT)
def onEventImportEnd(caller, event):
	windowTitle = os.path.basename(slicer.mrmlScene.GetURL())
	mainWindow = slicer.util.mainWindow()
	mainWindow.setWindowTitle(windowTitle)

@vtk.calldata_type(vtk.VTK_OBJECT)
def onEndClose(caller, event):
	mainWindow = slicer.util.mainWindow()
	mainWindow.setWindowTitle('Slicer')

slicer.mrmlScene.AddObserver(slicer.vtkMRMLScene.EndImportEvent, onEventImportEnd)
slicer.mrmlScene.AddObserver(slicer.vtkMRMLScene.EndCloseEvent, onEndClose)

```
