I've set up my tkinter app, built with pyinstaller, to display a splash screen on startup. Due to the intended use scenario of this app I have also set it up to permit only one app instance at a time. Before I instantiate my tkinter Root
class, I run my instance_check
function:
def instance_check() -> bool:
"""
Only permit one application instance at a time. if the app is already
running, restore and focus the existing window.
"""
user32 = ctypes.WinDLL('user32')
if hwnd := user32.FindWindowW(None, APP_NAME): # if a matching app window exists...
if not user32.IsZoomed(hwnd): # if the window isn't maximized...
user32.ShowWindow(hwnd, 1) # activate/restore the window
user32.SetForegroundWindow(hwnd) # focus the window
sys.exit() # bail - don't expect a return value
return True # otherwise: only one app instance found
e.g.:
if __name__ == '__main__':
if instance_check():
root = Root()
root.mainloop()
When starting the 1st app instance, everything works as expected - the app starts after the splash screen. Howevew, the splash screen is shown regardless of the return value of instance_check
. When starting another app instance after the 1st, the splash screen appears and then the existing window is focused (as intended).
I gather that this is because the splash screen is bundled into my pyinstaller build spec when I create my executable, so I'm wondering if there's any way to skip it if instance_check
fails. I fully expect the answer to be "No", but I figured it was worth asking. Any help is appreciated.
Additional, possibly irrelevant info: The app is built in Python 3.11 on Windows 10 and will only ever be run on Windows machines