Making keyboard shortcuts to open modules

Hi everyone!
I have been writing a python script that allows opening specific modules using keyboard shortcuts.
However, the script does not work quite right.
Below is what I could put together based on the forum topics.

#Makes selected module to popup as second module
def openSegmentEditor():
slicer.modules.segmenteditor.widgetRepresentation().setParent(None)
slicer.modules.segmenteditor.widgetRepresentation().show()

#setup the short cut
shortcut1 = qt.QShortcut(slicer.util.mainWindow())
shortcut1.setKey(qt.QKeySequence(“Ctrl+n”))
shortcut1.connect( ‘activated()’, openSegmentEditor)

This script allowed me to open the segment module as a second panel using ctrl+n.
However, the SegmentEditor hotkeys(numbers, shit+numbers) do not work when opened as a second module.

I am new to python, and I have been looking all over the forum but could not find the proper solution to this problem.
I would be grateful if anyone could help me out with this.

So my questions are:

  1. Is there any way to open the module on the main panel?
  2. I am currently planning to make a shortcut that would allow me to not only open the module but also lead me to the exact widget. For instance, I want to create a shortcut that would directly lead me to EraseInside of the Scissor from the main window. Would this be better, or should I just set up a toggle hotkey and switch around?

Thank you always for sharing your knowledge!

Try this :

#Makes selected module to popup as second module
def openSegmentEditor():
  mainWindow = slicer.util.mainWindow()
  mainWindow.moduleSelector().selectModule('SegmentEditor')

  
  
#setup the short cut
shortcut1 = qt.QShortcut(slicer.util.mainWindow())
shortcut1.setKey(qt.QKeySequence('Ctrl+n'))
shortcut1.connect('activated()', lambda: openSegmentEditor())

Use single quotes only : ‘activated()’ -> 'activated()'

1 Like

That worked beautifully! Thank you so much!!

May I ask one additional question?
https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html
In here, they used “activated()”.
May I ask why I should only use single quote?

In your first post, the ‘activated()’ signal is not between single quotes, but other similarly looking characters, at least the second ‘quote but not a quote’. By copy/paste in the python console, it won’t run. I did not try with double quotes, may be it’s OK. I meant : not other characters that look like single quote.

1 Like

Aha! I understand now! Thank you for your reply!

The quotation issue may be an “auto-correct”/rendering issue with the discourse forum, for text outside of ‘code’ demarcation boundaries…

# [...] an "auto-correct" issue with the discourse forum, for text outside of 'code' demarcation boundaries....
dummy = "text"

chir.set is right to warn about this, though, as it can lead to unexpected problems!

—DIV

1 Like