Skip to content

Instantly share code, notes, and snippets.

@sjh26
Last active August 9, 2022 14:50
Show Gist options
  • Save sjh26/1b9866e59a899d57818b424852df5ef1 to your computer and use it in GitHub Desktop.
Save sjh26/1b9866e59a899d57818b424852df5ef1 to your computer and use it in GitHub Desktop.
Embedding external app into slicer
def onConnectAppButton(self):
import win32gui
import win32con
import time
# Launch the application
proc = qt.QProcess()
proc.setProgram("/path/to/application")
proc.startDetached()
# Grab the splashscreen / startup window and minimize it
splashScreenHandle = 0
while splashScreenHandle == 0:
splashScreenHandle = win32gui.FindWindow(None, 'App')
time.sleep(.01)
win32gui.ShowWindow(splashScreenHandle, win32con.SW_MINIMIZE)
# Grab the actual window once available
windowHandle=0
while windowHandle == 0:
windowHandle = win32gui.FindWindow(None, 'App: With More stuff in title')
time.sleep(.01)
# Embed into slicer application
window = qt.QWindow.fromWinId(windowHandle)
windowContainer = slicer.util.mainWindow().createWindowContainer(window)
slicer.util.findChild(slicer.util.mainWindow(), 'AppWindowFrame').layout().addWidget(windowContainer)
# Show embedded application
self.toggleCenterView()
def toggleCenterView(self):
layoutVisible = slicer.util.findChild(slicer.util.mainWindow(), 'CentralWidgetLayoutFrame').visible
appVisible = slicer.util.findChild(slicer.util.mainWindow(), 'AppWindowFrame').visible
slicer.util.findChild(slicer.util.mainWindow(), 'CentralWidgetLayoutFrame').visible = not layoutVisible
slicer.util.findChild(slicer.util.mainWindow(), 'AppWindowFrame').visible = not appVisible
def callThisInSetupOfModuleToCreateFrameForApp(self):
centralWidget = slicer.util.findChild(slicer.util.mainWindow(), 'CentralWidget')
AppWindowFrame = qt.QFrame()
AppWindowFrame.objectName = 'AppWindowFrame'
AppWindowFrame.visible = False
AppWindowFrame.setLayout(qt.QVBoxLayout())
centralWidget.layout().addWidget(AppWindowFrame)
@sjh26
Copy link
Author

sjh26 commented Aug 5, 2022

Windows specific, Linux would use xterm for finding the window ID.

python package: pypiwin32

This example is also specific to an application that has a splashscreen / startup window that is not the one you want to embed

@sjh26
Copy link
Author

sjh26 commented Aug 9, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment