PySimpleGUI/Tk:在 Mac 上启动非常慢 [英] PySimpleGUI / Tk: very slow startup on Mac

查看:67
本文介绍了PySimpleGUI/Tk:在 Mac 上启动非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很多时间试图理解为什么在 Mac OS X 上使用 Tk 加载 PySimpleGUI 需要这么多时间.

更具体地说:

window.refresh()

需要9 秒来完成我的(非常简单的)完整 UI.我为调查减少了它,所以现在需要 3 秒.

这种情况发生大约 1/7 次,而其他时候通常很短.Windows 上的同一应用程序未显示此问题.

简化的用户界面是这样的:

布局如下:

顶部 (2)--- 行 ------->-  -  - - 柱子 ()--------|--- Btn相机"|Btn关闭"--------|------ 行 ------->------- 列(左列)--------|行 ------->--------|--- Btn4"|Btn10"|Btn20"|Btn40"|--------|---- 列 ()--------|-----|--- CB比例尺"--------|-----|--- CB白条"--------|行 ------->--------|--- BtnBrFd"|BtnDAPI"|BtnFITC"|BtnCY3"--------|行 ------->--------|--- 多行 |Btn"--------|行 ------->--------|--- 多行 |Btn"--------|行 ------->--------|--- Btn"|Btn显示最后"|Btn保存"|Btn出口IJ"--------|行 ------->--------|--- Btn导出 TIF"|Btn删除选中"

我用分析器运行它:

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

然后使用此代码:

导入 pstats从 pstats 导入 SortKeyp = pstats.Stats('prof')p.sort_stats(SortKey.CUMULATIVE).print_stats(10)

获得此分析:

<前>2021 年 4 月 29 日星期四 15:10:39 教授204224 次函数调用(199897 次原始调用)在 4.099 秒内订购者:累计时间由于限制,列表从 2141 个减少到 10 个ncalls tottime percall cumtime percall filename:lineno(function)552/1 0.003 0.000 4.100 4.100 {内置方法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 {_tkinter.tkapp"对象的调用"方法}1 0.000 0.000 2.978 2.978/Users/u/opt/miniconda3/envs/camera/lib/python3.7/site-packages/PySimpleGUI/PySimpleGUI.py:8247(刷新)1 0.000 0.000 2.978 2.978/Users/u/opt/miniconda3/envs/camera/lib/python3.7/tkinter/__init__.py:1175(更新)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)

问题线似乎是这个:

 393/389 3.170 0.008 3.170 0.008 {'_tkinter.tkapp'对象的'调用'方法}

如果您能提供有关此问题的任何线索,我将不胜感激!

解决方案

对此我没有很好的解释,但感谢 @lenka_cizkova 添加的评论,我现在可以提供解决方法:

通过添加启动画面,即使是很短的(100ms),主画面也会立即加载:

# 在 Mac OSX 上消除长加载时间的闪屏如果 os.name != 'nt':SPLASH_IMAGE_FILE = 'img/my-logo.png'DISPLAY_TIME_MILLISECONDS = 100sg.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)

此代码应在构建和显示主窗口之前放置.

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:

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!

解决方案

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.

这篇关于PySimpleGUI/Tk:在 Mac 上启动非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆