Collapse/Expand Widget Buttons using Python

Hello all,

I am developing a new module using the Scripted Module template. I would like to be able collapse/expand the collapsible buttons in the Widget using Python. Specifically, I would like to have some of the buttons collapsed using a python script at startup (using the --python-script option when starting Slicer).

I can find the ctkCollapsibleButton instance, eg (using Slicer Radiomics module as an example):

slicer.modules.SlicerRadiomicsWidget.layout.itemAt(0).widget()

According to CTK documentation, the ctkCollapsibleButton should have a method setCollapsed() ; however, I get an error saying the method does not exist:

slicer.modules.SlicerRadiomicsWidget.layout.itemAt(0).widget().setCollapsed(True)

Traceback (most recent call last):
File “<console>”, line 1, in <module>
AttributeError: ctkCollapsibleButton has no attribute named ‘setCollapsed’

I would appreciate if anyone could tell me how I can collapse/expand the buttons in the widget.

Edit: I am using Slicer 4.10.0.

Thanks in advance,
ebp

Qt object properties are get/set as attributes in Python:

slicer.modules.SlicerRadiomicsWidget.layout.itemAt(0).widget().collapsed = True

Note that such low-level hijacking of a module’s GUI would be extremely fragile (if a new item is added to the GUI, references like the one above would be broken). Instead, it would be a much better approach to make the widget customizable (for example, add a method to show/hide some details) or move creation of a widget to a method that you can call from outside of the module to generate a widget section. You can make whatever change you would like in the module and then send a pull request to the module’s maintainer.

What would you like to do?
Why would you need to collapse a section?
Don’t you want a more streamlined user interface (have all the GUI that you need for your workflow in a single module)?

Hi Andras,

Thank you for the information. I certainly agree that the sample code I showed is poorly implemented. I just copied/pasted from the Python Interactor while I was experimenting around with the GUI. I have to admit that I am not very familiar with Qt; however, and at the moment, I am not certain how to reference the collapsible buttons more generally. Regardless, the line of code I posted would certainly not be the final implementation.

To answer your other questions, the module is intended to be used as part of a surgeon video console, which is split across multiple screens (the main window is full screen on one monitor, the module GUI is floating on another). The module is large, and it has several sections that need to be used in sequential order. I would like the module to launch with certain sections collapsed because those sections are never used immediately upon launching the module.

I hope that clarifies things. I’m certain there is room to improve the GUI design (I’ve never made a GUI before), so if you have any tips or resources, I would be glad to hear them. If you are interested, I could probably share more specific information with you privately.

Thanks again for your help,
Bryn

We implemented several 3D Slicer based applications that are used by surgeons and various other specialists for interventional guidance (using overhead monitors, projectors, touchscreens, and augmented reality displays). Due to the huge amount of pre-built software componentsin 3D Slicer, SlicerIGT, and other extensions, it is typically feasible to build an initial application prototype within a week or so and then you can do quick iterations based on user feedback to get practically usable system in a few month. A couple of examples are listed here: http://www.slicerigt.org/wp/examples/

We would be happy to share how we implemented these, but we would need to know much more about what you plan to do:
Do you use real-time imaging (ultrasound, fluoroscopy, MRI, etc.)?
Do you use tool navigation (surgical navigation systems or optical or electromagnetic trackers)?
How surgeons interact with the system (touchscreen, LeapMotion, IMU, tracked tools, …)?
What information do you need to display on how many screens?
Do you need any spatial or temporal calibration?

The most efficient could be if you can attend one of the Slicer project weeks where you can probably build a first prototype with help from Slicer experts and other participants who have developed similar systems. You can also explore examples, tutorials, source code of existing systems, and of courseyou can also keep posting specific questions on the forum.