Hi to everyone!
version: 5.6.2
OS: windows11
I’m working in create a robust (but also simple) interface for our python module named SegmentTracer. This code contains several classes and create/remove buttons begins to be hard.
Watching some Scripted Modules as could be Endoscopy I see that the most common way to do that is using a classUI to manage all the functions related with the UI. I am not familiarized with ScriptedLoadableModuleWidget
but I have made a complete Qt integration:
import slicer
import qt
class SegmentTracerUI:
def __init__(self):
self.dock_widget = slicer.util.mainWindow().findChild(qt.QDockWidget,"SegmentTracerDockWidget")
if self.dock_widget:
self.dock_layout = self.dock_widget.findChild(qt.QWidget).layout()
self.label = self.dock_widget.findChild(qt.QLabel)
else:
self.main_window.setStatusBar(None)
self.create_dock_widget()
def create_dock_widget(self, title="ST Assistant"):
"""Crea el dock widget y lo agrega a la interfaz."""
self.dock_widget = qt.QDockWidget(title)
self.dock_widget.setObjectName("SegmentTracerDockWidget")
# Contenedor y layout
dock_content = qt.QWidget()
self.dock_layout = qt.QVBoxLayout(dock_content)
# Label de bienvenida
self.label = qt.QLabel("Welcome to SegmentTracer.")
self.label.setFont(qt.QFont("Times", 11))
self.dock_layout.addWidget(self.label)
self.dock_widget.setWidget(dock_content)
self.main_window.addDockWidget(qt.Qt.LeftDockWidgetArea, self.dock_widget)
def create_button(self, function, text):
"""Crea un botón y lo añade al dock."""
button = qt.QPushButton(text)
button.clicked.connect(function)
self.dock_layout.addWidget(button)
slicer.app.processEvents()
return button
def clear_buttons(self):
"""Elimina todos los botones del dock, preservando solo el QLabel."""
for i in reversed(range(self.dock_layout.count())):
widget = self.dock_layout.itemAt(i).widget()
if widget and widget is not self.label:
widget.setParent(None)
widget.deleteLater()
slicer.app.processEvents()
def show_message(self, text):
"""Cambia el texto del QLabel dentro del dock."""
self.label.setText(text)
print(text)
slicer.app.processEvents()
What I am wondering is:
1º Can I modify something to guarantee a better user experience? (i.e. not blocking the Slicer window)
2º Also, to link one function to a specific button I use:
self._continue_action = action_creator(myFunction, event_type = 'Continue', args = [myFunctionArg1,myFunctionArg2,...])
self.button_continue = ui.create_button(self.button_continue_action, 'Continue')
where the function action_creator is the following function:
def action_creator(self, function, event_type, args = []):
if event_type == 'Continue':
slicer.app.processEvents()
def action():
if len(args) > 0:
function(*args)
else:
function()
return None
return action
Which looks a little bit difficult… Has anyone any idea to associate the button with the functions and their args?
Thanks a lot!