tkinterpysimpleguicprofile

PySimpleGUI / Tk: very slow startup on Mac


I was spending a lot of time trying to understand why loading time of PySimpleGUI with Tk on Mac OS X takes so much time.

To be more specific:

window.refresh()

takes 9 seconds for my (quite simple) full UI. I reduced it for the investigation, so now it takes 3 seconds.

It is happening about 1/7 of the times, while on the other times it is normally short. Same app on Windows does not show this problem.

The reduced UI is this:

enter image description here

The layout goes like this:

Top (2)
--- Row ------->
------- Column ()
--------|--- Btn "Camera" | Btn "Close"
--------|--- 
--- Row ------->
------- Column (left-col)
--------| Row ------->
--------|--- Btn "4" | Btn "10" | Btn "20" | Btn "40" | 
--------|---- Column ()
--------|-----|--- CB "Scale Bar"
--------|-----|--- CB "White Bar"
--------| Row ------->
--------|--- Btn "BrFd" | Btn "DAPI" | Btn "FITC" | Btn "CY3"
--------| Row ------->
--------|--- Multiline | Btn ""
--------| Row ------->
--------|--- Multiline | Btn ""
--------| Row ------->
--------|--- Btn "" | Btn "Show Last" | Btn "Save" | Btn "Export IJ"
--------| Row ------->
--------|--- Btn "Export TIF" | Btn "Delete Checked"

I was running it with a profiler:

rm prof && python -m cProfile -o prof uscope.py

then used this code:

import pstats
from pstats import SortKey
p = pstats.Stats('prof')
p.sort_stats(SortKey.CUMULATIVE).print_stats(10)

to get this analysis:

Thu Apr 29 15:10:39 2021    prof

         204224 function calls (199897 primitive calls) in 4.099 seconds

   Ordered by: cumulative time
   List reduced from 2141 to 10 due to restriction 

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    552/1    0.003    0.000    4.100    4.100 {built-in method builtins.exec}
        1    0.000    0.000    4.100    4.100 uscope.py:24()
        1    0.000    0.000    3.460    3.460 /Users/u/Prog/c/pbio-printer/camera2/VerySimpleGUI2.py:335(build_and_run)
  393/389    3.170    0.008    3.170    0.008 {method 'call' of '_tkinter.tkapp' objects}
        1    0.000    0.000    2.978    2.978 /Users/u/opt/miniconda3/envs/camera/lib/python3.7/site-packages/PySimpleGUI/PySimpleGUI.py:8247(refresh)
        1    0.000    0.000    2.978    2.978 /Users/u/opt/miniconda3/envs/camera/lib/python3.7/tkinter/__init__.py:1175(update)
   278/10    0.002    0.000    0.638    0.064 :978(_find_and_load)
   277/10    0.001    0.000    0.638    0.064 :948(_find_and_load_unlocked)
   386/11    0.000    0.000    0.636    0.058 :211(_call_with_frames_removed)
    258/8    0.001    0.000    0.635    0.079 :663(_load_unlocked)

the problematic line seem to be this one:

 393/389    3.170    0.008    3.170    0.008 {method 'call' of '_tkinter.tkapp' objects}

I'd be very thankful for any clue regarding this problem!


Solution

  • I do not have a good explanation for this, but thanks to the comment added by @lenka_cizkova, I can now offer a workaround:

    By adding a splash screen, even a very short one (100ms), the main screen is loaded immediately:

    # Splash screen to eliminate long loading time on Mac OSX
    if os.name != 'nt':
        SPLASH_IMAGE_FILE = 'img/my-logo.png'
        DISPLAY_TIME_MILLISECONDS = 100
        sg.Window('',
                  [[sg.Image(SPLASH_IMAGE_FILE)]], transparent_color=sg.theme_background_color(),
                  no_titlebar=True, keep_on_top=True).read(timeout=DISPLAY_TIME_MILLISECONDS, close=True)
    

    This code should be placed before building and showing the main window.