# Customizing Python after extension wizard

**URL:** <https://discourse.slicer.org/t/customizing-python-after-extension-wizard/26751>\
**Category:** Development\
**Created:** [December 15, 2022, 4:39pm UTC](https://discourse.slicer.org/t/customizing-python-after-extension-wizard/26751 "2022-12-15T16:39:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lee\_Newberg](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lee_newberg/32/7615_2.png) [@Lee\_Newberg](https://discourse.slicer.org/u/Lee_Newberg)\
**Post date:** [December 15, 2022, 4:39pm UTC](https://discourse.slicer.org/t/customizing-python-after-extension-wizard/26751/1 "2022-12-15T16:39:52Z")

</div>

I used the Slicer Extension Wizard to create two modules for my custom Slicer application. Instead of the `inputSelector (qMRMLNodeComboBox)`, `outputSelector (qMRMLNodeComboBox)`, `imageThresholdSliderWidget (ctkSliderWidget)`, `invertOutputCheckBox (QCheckBox)`, and `invertedOutputSelector (qMRMLNodeComboBox)`. which are supplied as examples, I will have `DataDirectory (ctkPathLineEdit)` and `PatientPrefix (QLineEdit)`.

I have some pretty good ideas on how to modify the `MyNewModule.ui` file accordingly. However, I am having trouble with the `MyNewModule.py` file. For example, what should the ` self.ui.DataDirectory.connect(...)` call look like? (I am not that clear on what this does which likely is a big part of my problem.) Likewise what should the arguments for `self.ui.PatientPrefix.connect(...)` be? Thanks!

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [December 15, 2022, 5:14pm UTC](https://discourse.slicer.org/t/customizing-python-after-extension-wizard/26751/2 "2022-12-15T17:14:28Z")

</div>

Have a read of this old post of mine seen below. I generally prefer the new style Qt signal/slot method of **{object}.{signal\_name}.connect({slot\_name})**

> [@How to use signals and slots in Slicer 3d?](https://discourse.slicer.org/t/how-to-use-signals-and-slots-in-slicer-3d/14013/2):
>
> A previous post of mine mentioned some about basics of using Qt signals and slots with python. Essentially the syntax is something like {object}.{signal\_name}.connect({slot\_name}). So something that is based on QProgressBar will have [these signals](https://doc.qt.io/qt-5/qprogressbar.html#signals) such as valueChanged. Therefore def printMyNewValue(value): print("The progress bar value is now: {}".format(value)) import qt progress\_bar = qt.QProgressBar() progress\_bar.setMaximum(10) progress\_bar.valueChanged.connect(printMyNewValue) progre…

---

<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:** [December 15, 2022, 8:04pm UTC](https://discourse.slicer.org/t/customizing-python-after-extension-wizard/26751/3 "2022-12-15T20:04:39Z")

</div>

> [@Lee\_Newberg](#):
>
> For example, what should the ` self.ui.DataDirectory.connect(...)` call look like?

It creates a connection between a Qt signal and a Python method. When the Qt object emits the signal then it calls the connected Python method. You can find the specification of signals in the header files or the generated documentation. Unfortunately, [CTK documentation site is down](https://github.com/commontk/CTK/issues/919), so you need to look up the signal parameters in source code:

- google for ctkPathLineEdit, click on the [first hit](https://github.com/commontk/CTK/blob/master/Libs/Widgets/ctkPathLineEdit.h)
- search for `signals`
- probably you are interested in this signal:

> <https://github.com/commontk/CTK/blob/1abb81ed5cf0d91827518c55f73047d8fcc870f2/Libs/Widgets/ctkPathLineEdit.h#L250>

- the connection code will look like this:

```python
self.ui.DataDirectory.connect('currentPathChanged(QString)', self.someFunction)

```

Since Slicer is open-source and there are about 200 open-source extensions, mostly developed in Python, hosted on github, an alternative approach is to learn from working examples. You can find example for most API calls, for example you can [search for `ctkPathLineEdit currentPathChanged` in Python code in entire github](https://github.com/search?l=Python&q=ctkPathLineEdit+currentPathChanged&type=Code). One of the hits show how to create the connection:

> <https://github.com/pieper/SlicerImageStacks/blob/76e40cf61a68cdab5cc7802e61cc3e0ee9b5acca/ImageStacks/ImageStacks.py#L170>

---

<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:** [December 15, 2022, 8:32pm UTC](https://discourse.slicer.org/t/customizing-python-after-extension-wizard/26751/4 "2022-12-15T20:32:25Z")

</div>

I’ve added the information from @jamesobutler and my post to the Python FAQ:

> <https://github.com/Slicer/Slicer/pull/6737>
>
> This should help with questions like this: https://discourse.slicer.org/t/custom…izing-python-after-extension-wizard/26751/3

Please have a look and suggest improvements that would help other developers.
