当前位置:网站首页>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创新
边栏推荐
- leetcode每日一练:旋转数组
- Design of electronic clock based on 51 single chip microcomputer
- Which is more cost-effective, haqu K1 or haqu H1? Who is more worth starting with?
- 漫画:如何实现大整数相乘?(上) 修订版
- BigDecimal除法的精度问题
- CVPR 2022 best student paper: single image estimation object pose estimation in 3D space
- Tita 绩效宝:如何为年中考核做准备?
- 7. Scala class
- Humi analysis: the integrated application of industrial Internet identity analysis and enterprise information system
- Winedt common shortcut key modify shortcut key latex compile button
猜你喜欢
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
解决“双击pdf文件,弹出”请安装evernote程序
Design of electronic clock based on 51 single chip microcomputer
33: Chapter 3: develop pass service: 16: use redis to cache user information; (to reduce the pressure on the database)
MySQL queries the latest qualified data rows
MATLAB查阅
flask接口响应中的中文乱码(unicode)处理
Kafaka技术第一课
Summary of optimization scheme for implementing delay queue based on redis
Redis+caffeine two-level cache enables smooth access speed
随机推荐
ICML 2022 | Meta propose une méthode robuste d'optimisation bayésienne Multi - objectifs pour faire face efficacement au bruit d'entrée
The comprehensive competitiveness of Huawei cloud native containers ranks first in China!
Ant financial's sudden wealth has not yet begun, but the myth of zoom continues!
2022年信息系统管理工程师考试大纲
Disabling and enabling inspections pycharm
QT console printout
力扣解法汇总729-我的日程安排表 I
Compter le temps d'exécution du programme PHP et définir le temps d'exécution maximum de PHP
Knowledge points of MySQL (7)
[binary tree] insufficient nodes on the root to leaf path
使用QT设计师界面类创建2个界面,通过按键从界面1切换到界面2
Humi analysis: the integrated application of industrial Internet identity analysis and enterprise information system
Check the WiFi password connected to your computer
漫画:寻找股票买入卖出的最佳时机
mysql如何使用JSON_EXTRACT()取json值
基于51单片机的电子时钟设计
CVPR 2022 best student paper: single image estimation object pose estimation in 3D space
云安全日报220705:红帽PHP解释器发现执行任意代码漏洞,需要尽快升级
SQL Server(2)
漫画:寻找无序数组的第k大元素(修订版)