How to call a function from the logic

python
(Helen Siampli) #1

Hello,

I was wondering how can I call correctly a function that is in the ModuleNameLogic inherited by ScriptedLoadableModuleLogic in python?
Currently, I try the following with no success:

slicer.modules.moduleName.logic().Test()

My current code seems like the following and I want to call from Python Interactor the Test() method:

> class ModuleName(ScriptedLoadableModule):
>     def __init__(self, parent):
>     ScriptedLoadableModule.__init__(self, parent)
> 
> 
> class ModuleNameWidget(ScriptedLoadableModuleWidget):
>   
>   def setup(self):
>     ScriptedLoadableModuleWidget.setup(self)
> 
> 
> class ModuleNameLogic(ScriptedLoadableModuleLogic):
>   
>   def Test(self):
>     print("Test is called")
>     pass

Thanks for your help

0 Likes

(Steve Pieper) #2

Hi @siaeleni -

You should be able to do:

ModuleNameLogic().Test()

if you are in a different module file or in the python interactor you might need to call import ModuleName first.

(As an aside, the other ‘logic’ class slicer.modules.moduleName.logic is actually the C++ loadable module instance corresponding to the scripted module, but it’s really just an internal structure and doesn’t have any particular utility here).

-Steve

0 Likes

(Andras Lasso) #3

I usually create a single logic instance in the widget that I can access via the widget class:

slicer.modules.screencapture.widgetRepresentation().self().logic

Should we update updating the scripted module template to create a logic instance in the widget’s constructor instead of creating a new logic instance each time? This would make the Python and C++ modules a bit more consistent.

1 Like

(Helen Siampli) #4

I created an instance and use that to access to the logic methods.Thanks a lot!

0 Likes

(Ryley Feeney) #5

Is this a valid Solution?

0 Likes