Accessing the Module Window

Hi there,

I was wondering if there was any way to access the currently running module and its corresponding widgets through code. By this, I mean for example if I were running the Segmentation module, is it possible for me to access the module that is on the display menu, and then isolate the SliceFill widget that is present in the module?

Thank you very much for the help.

Yes, you can access any module from any other module.

A module must not use another module’s GUI (the GUI is only for user interactions), but instead a module should create/modify MRML nodes and call module logic functions.

Specifically about Segment Editor - you can find here are examples for running Segment Editor effects from a Python script:
https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How_to_run_segment_editor_effects_from_a_script

I used the segmentation editor as an example but am more specifically looking to pull the widgets out of the Reformat module. I’m having a bit of trouble locating the module (i.e, I am able to call the widgets present on the mainWindow() but could not find access to the module window) and as such, cannot find the widgets that the Reformat module has built in.

You cannot reuse the module’s main GUI widgets, but usually a module widget consists of reusable widgets. How to find a widget:

In Reslice module’s case, search result shows that the button is in a module widget (qSlicerReformatModuleWidget.ui), so the widget is not reusable. If you are familiar with C++ then you can refactor the code to move the part you need to a separate reusable widget and use that widget in your module. However, it may be simpler to just have a look at the module widget’s code and reimplement in your code whatever you need.

Reslicing a volume using sliders is really simple to implement. See for example how it is done in ValveView module in SlicerHeart extension: https://github.com/SlicerHeart/SlicerHeart/blob/master/ValveView/ValveView.py

You cannot reuse the module’s main GUI widgets, but usually a module widget consists of reusable widgets.

To complete the answer of @lassoan, we introduced a method named createNewWidgetRepresentation to support creating new module representation.

In your case, the following would work:

reformatModuleWidget = slicer.modules.reformat.createNewWidgetRepresentation()
reformatModuleWidget.setMRMLScene(slicer.app.mrmlScene())
reformatModuleWidget.show()

That said, your module and workflow would be dependent on the layout of that specific module.

@jcfr in the scenario that you have stated above, does this not take the entire reformat module and simply create a widget elsewhere? in which case the individually packaged components would still not be retrievable, is that correct?

@lassoan thank you for the response! i will certainly take a look into the code and try to develop the widget after i figure out all the individual components of it

This is correct, you would get the following:

reformat-ui-standalone

Then, if you only want to hide part of it, you could do something like this:

slicer.util.findChild(reformatModuleWidget, "OriginCoordinatesGroupBox").hide()

and you would get:

reformat-ui-standalone-tweaked

Name of the object to access is found in the associated UI file: qSlicerReformatModuleWidget.ui

This approach is suitable for fast prototyping and experimenting with UI. After, an approach is validated, I suggest to refactor the module so that you could reuse its components.

thank you so much, this has been extremely helpful