# Avoid creating multiple module widget instances from a python script

**URL:** <https://discourse.slicer.org/t/avoid-creating-multiple-module-widget-instances-from-a-python-script/36717>\
**Category:** Support\
**Tags:** python, vmtk, vmtk-centerline\
**Created:** [June 12, 2024, 10:45am UTC](https://discourse.slicer.org/t/avoid-creating-multiple-module-widget-instances-from-a-python-script/36717 "2024-06-12T10:45:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![RomanStriker](https://avatars.discourse-cdn.com/v4/letter/r/c37758/32.png) [@RomanStriker](https://discourse.slicer.org/u/RomanStriker)\
**Post date:** [June 12, 2024, 10:45am UTC](https://discourse.slicer.org/t/avoid-creating-multiple-module-widget-instances-from-a-python-script/36717/1 "2024-06-12T10:45:36Z")

</div>

Hi,  
I am trying to generate centerlines for a batch of segmentation masks. I use a python script that uses the VMTK Extract Centerline widget. The issue is that for every new segmentation, Slicer opens a new Extract Centerline widget window, while keeping the previous window open as well, leading to a lot of windows, as the script processes more masks. The script has the following structure.

```auto
for volume, segmentation in zip(volume_list, segmentation_list):
# 1. load volume and segmentation
...

# 2. Auto-detect the endpoints and extract centerline
extractCenterlineWidget = ExtractCenterline.ExtractCenterlineWidget()
# Set the parameters
extractCenterlineWidget._parameterNode.SetNodeReferenceID("InputSurface", seg.GetID())
extractCenterlineWidget._parameterNode.SetParameter("InputSegmentID", segmentID)
...
extractCenterlineWidget.onAutoDetectEndPoints()
extractCenterlineWidget.onApplyButton()

# Save centerline
...

# Clean up
slicer.mrmlScene.Clear(0)

# Hoping this would exit slicer completely and start 
# again for the next iteration of the loop but it does not.
slicer.app.exit()

```

How to avoid opening multiple widget windows or close the previous one when the processing for the current segmentation is finished?

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [June 12, 2024, 10:55am UTC](https://discourse.slicer.org/t/avoid-creating-multiple-module-widget-instances-from-a-python-script/36717/2 "2024-06-12T10:55:08Z")

</div>

The simplest solution is to re-use the same widget and only change the parameter node content before calling apply again.
