当前位置:网站首页>tkinter窗口预加载
tkinter窗口预加载
2022-07-05 17:17:00 【Smart-Space】
引言
前提说明,这不是一篇技术文章,只是一个经验分享。
问题来源
众所周知,tkinter作为Python的标准库GUI库,有一个问题就是加载比较慢,相关的实验也基本证明了这一点。但是,在你的项目中,导致窗口启动慢的真的是tkinter窗口自身的原因吗?这可不一定。
一般地tkinter使用
首先,让我们先回忆众多tkinter项目是如何在其中写tkinter窗口的出现时机的。
该import的import
加载类以及函数
确定与tkinter窗口相关的变量
加载tkinter窗口
很明显,在tkinter窗口出现前,Python还要完成一些工作。其中,第一步和第三步会产生较长的耗时。原因是,有一些库真的复杂,如PIL、Panda等,这些都有专门的pyd以及动态链接库要导入,并且自身就有很多源文件;另一方面,tkinter窗口需要创建众多控件,特别是较复杂的项目,创建控件就需要一会时间。
在创建控件这一方面,customtkinter做得挺好,采用加载后渲染方式;TinUI的渲染加载速度很快。
在我的一个业余项目中,也就是TinReader-TinGroup,要加载内容还挺多,基本需要3~6秒。
一个榜样
你看看隔壁火狐,不就是不管三七二十一,先给你显示一个窗口框架(背景)吗?别人浏览器的预先加载量可是十分大的。
既然有这么多事要做,我还能做什么么呢,我该这么办呢?
其实很简单,就是把tkinter最先导入,下一行立马显示tkinter窗口。
wait,我没开玩笑。
具体办法
直接显示窗口
要做的改变很少。具体就如下:
#导入tkinter
from tkinter import *
#必要变量
#title=...
#width=...
#height=...
#使用高DPI...
#创建窗口
root=Tk()
root.geometry('500x500')
root.update()
#后续操作
注释已经写清楚了。
我自己的项目
在TinReader源码中,总共有1195行,其中需要载入pythonnet, PIL, tkwebview2, 各个依赖项;获取初始化设置等等。大概要耗3~6秒的时间。
现在,直接在开头:
# -*- coding: utf-8 -*-
from tkinter import Tk,Label,Frame,StringVar,scrolledtext,IntVar,Message,Radiobutton,Menu,Checkbutton,Canvas,\
Button as tkButton,LabelFrame,Toplevel,PhotoImage,Entry as tkEntry
import ctypes
#先导界面---
ctypes.windll.shcore.SetProcessDpiAwareness(1)
TinTop=Tk()#主窗口
TinTop.withdraw()
sw = TinTop.winfo_screenwidth()
#得到屏幕宽度
sh = TinTop.winfo_screenheight()
#得到屏幕高度
root=Toplevel()
root.geometry(str(sw//2-50)+'x'+str(sh-130)+'+0+10')
root.update()
#---
效果

如果没有这个顺序转化,那么从开始运行、tkinter空白窗口、tkinter窗口加载完毕这段时间,本来都是空空如也,使用甚至可能会感觉死机了。
源码确确实实是1000+行的代码,中间的步骤也确实比较耗时,现在,至少看到一个tkinter空白窗口都比什么都没有要好受。
结语
本篇只做经验分享。
建议使用的项目:初始化耗时的tkinter项目。
tkinter创新
边栏推荐
猜你喜欢
Redis+caffeine two-level cache enables smooth access speed

Seven Devops practices to improve application performance

33: Chapter 3: develop pass service: 16: use redis to cache user information; (to reduce the pressure on the database)

Beijing internal promotion | the machine learning group of Microsoft Research Asia recruits full-time researchers in nlp/ speech synthesis and other directions
In depth understanding of redis memory obsolescence strategy

VBA drives SAP GUI to realize office automation (II): judge whether elements exist

ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise

Knowledge points of MySQL (6)
What are the precautions for MySQL group by
Complete solution instance of Oracle shrink table space
随机推荐
读libco保存恢复现场汇编代码
Learn about MySQL transaction isolation level
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
Rider set the highlighted side of the selected word, remove the warning and suggest highlighting
MySQL之知识点(七)
Compter le temps d'exécution du programme PHP et définir le temps d'exécution maximum de PHP
To solve the problem of "double click PDF file, pop up", please install Evernote program
Debug kernel code through proc interface
这个17岁的黑客天才,破解了第一代iPhone!
漏洞复现----48、Airflow dag中的命令注入(CVE-2020-11978)
Is it safe and reliable to open futures accounts on koufu.com? How to distinguish whether the platform is safe?
Complete solution instance of Oracle shrink table space
ThoughtWorks global CTO: build the architecture according to needs, and excessive engineering will only "waste people and money"
mysql如何使用JSON_EXTRACT()取json值
Knowing that his daughter was molested, the 35 year old man beat the other party to minor injury level 2, and the court decided not to sue
论文阅读_中文NLP_LTP
如何保存训练好的神经网络模型(pytorch版本)
flask接口响应中的中文乱码(unicode)处理
MySql 查询符合条件的最新数据行
Kafaka技术第一课