当前位置:网站首页>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创新
边栏推荐
- MySql 查询符合条件的最新数据行
- 统计php程序运行时间及设置PHP最长运行时间
- Which is more cost-effective, haqu K1 or haqu H1? Who is more worth starting with?
- 华为云云原生容器综合竞争力,中国第一!
- 查看自己电脑连接过的WiFi密码
- Cloud security daily 220705: the red hat PHP interpreter has found a vulnerability of executing arbitrary code, which needs to be upgraded as soon as possible
- About JSON parsing function JSON in MySQL_ EXTRACT
- SQL删除重复数据的实例教程
- IDEA 项目启动报错 Shorten the command line via JAR manifest or via a classpath file and rerun.
- 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
猜你喜欢

提高应用程序性能的7个DevOps实践

MySQL之知识点(六)

Knowledge points of MySQL (7)

To solve the problem of "double click PDF file, pop up", please install Evernote program

Kafaka技术第一课

北京内推 | 微软亚洲研究院机器学习组招聘NLP/语音合成等方向全职研究员

33:第三章:开发通行证服务:16:使用Redis缓存用户信息;(以减轻数据库的压力)

WR | Jufeng group of West Lake University revealed the impact of microplastics pollution on the flora and denitrification function of constructed wetlands
mysql中取出json字段的小技巧

MySQL之知识点(七)
随机推荐
漫画:一道数学题引发的血案
Use QT designer interface class to create two interfaces, and switch from interface 1 to interface 2 by pressing the key
证券网上开户安全吗?证券融资利率一般是多少?
WebApp开发-Google官方教程
leetcode每日一练:旋转数组
Cartoon: looking for the k-th element of an unordered array (Revised)
漏洞复现----48、Airflow dag中的命令注入(CVE-2020-11978)
一文了解MySQL事务隔离级别
MATLAB查阅
力扣解法汇总729-我的日程安排表 I
Cartoon: how to multiply large integers? (integrated version)
网络威胁分析师应该具备的十种能力
蚂蚁金服的暴富还未开始,Zoom的神话却仍在继续!
How MySQL uses JSON_ Extract() takes JSON value
Server configuration jupyter environment
Summary of optimization scheme for implementing delay queue based on redis
Error in compiling libssh2. OpenSSL cannot be found
查看自己电脑连接过的WiFi密码
Short the command line via jar manifest or via a classpath file and rerun
Disabling and enabling inspections pycharm