# Call a widget's methods in Python

**URL:** <https://discourse.slicer.org/t/call-a-widgets-methods-in-python/33416>\
**Category:** SlicerDMRI\
**Tags:** python, testing, extensions\
**Created:** [December 16, 2023, 8:03pm UTC](https://discourse.slicer.org/t/call-a-widgets-methods-in-python/33416 "2023-12-16T20:03:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhlegarreta](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jhlegarreta/32/66542_2.png) [@jhlegarreta](https://discourse.slicer.org/u/jhlegarreta)\
**Post date:** [December 16, 2023, 8:03pm UTC](https://discourse.slicer.org/t/call-a-widgets-methods-in-python/33416/1 "2023-12-16T20:03:35Z")

</div>

Hello,

For the purposes of building a test, I am wondering whether there is a way to instantiate and call the methods from a class that inherits from `qSlicerWidget`​ (e.g. [`qSlicerTractographyDisplayWidget`​](https://github.com/SlicerDMRI/SlicerDMRI/blob/master/Modules/Loadable/TractographyDisplay/Widgets/qSlicerTractographyDisplayWidget.h#L22), which lives in [SlicerDMRI](https://github.com/SlicerDMRI/SlicerDMRI), and which has its Qt Designer UI file [here](https://github.com/SlicerDMRI/SlicerDMRI/blob/master/Modules/Loadable/TractographyDisplay/Widgets/Resources/UI/qSlicerTractographyDisplayWidget.ui)) programmatically from Python.

I am able to do the below:

```auto
m = slicer.util.mainWindow()
m.moduleSelector().selectModule("TractographyDisplay")
display_widget = slicer.util.findChildren(name="qSlicerTractographyDisplayModuleWidget")[0]

```

but when trying to call any of the `qSlicerTractographyDisplayWidget` methods, e.g.:

```auto
opacity = display_widget.opacity()
display_widget.updateWidgetFromMRML()

```

it looks like I am querying the wrong object:

```auto
AttributeError: qSlicerTractographyDisplayWidget has no attribute named 'opacity'

AttributeError: qSlicerTractographyDisplayWidget has no attribute named 'updateWidgetFromMRML'

```

What am I missing?

Thanks.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [December 16, 2023, 10:06pm UTC](https://discourse.slicer.org/t/call-a-widgets-methods-in-python/33416/2 "2023-12-16T22:06:04Z")

</div>

Hi - Qt methods are not wrapped by default. Only things like slots and properties are available unless you put `Q_INVOKABLE` in front of the methods name you want to make available.

---

<div class="post-metadata">

**Author:** ![jhlegarreta](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jhlegarreta/32/66542_2.png) [@jhlegarreta](https://discourse.slicer.org/u/jhlegarreta)\
**Post date:** [December 16, 2023, 10:28pm UTC](https://discourse.slicer.org/t/call-a-widgets-methods-in-python/33416/3 "2023-12-16T22:28:48Z")

</div>

Thanks Steve.

Maybe I’m wrong, but calling a public slot, e.g. `setVisibility`:

> <https://github.com/SlicerDMRI/SlicerDMRI/blob/master/Modules/Loadable/TractographyDisplay/Widgets/qSlicerTractographyDisplayWidget.h#L54C8-L54C21>

also results in the same error. Is there something else that I am missing?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [December 17, 2023, 11:57am UTC](https://discourse.slicer.org/t/call-a-widgets-methods-in-python/33416/4 "2023-12-17T11:57:15Z")

</div>

There must be something wrong with either the wrapping or the way the wrapping libraries are being loaded in the extension. You could look at how things work in the core, where QObject signals/slots/properties/invokables are available in python. Maybe also look at other extensions that provide QObject/QWidget classes with wrapped methods. It could be something in the way the cmake files are set up.
