当前位置:网站首页>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 queries the latest qualified data rows
- 統計php程序運行時間及設置PHP最長運行時間
- MySQL之知识点(七)
- To solve the problem of "double click PDF file, pop up", please install Evernote program
- 每日一练:关于日期的一系列
- Winedt common shortcut key modify shortcut key latex compile button
- 解读:如何应对物联网目前面临的安全问题?
- 7 pratiques devops pour améliorer la performance des applications
- MySQL之知识点(六)
- ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
猜你喜欢
基于Redis实现延时队列的优化方案小结

MATLAB查阅

VBA drives SAP GUI to realize office automation (II): judge whether elements exist
MySql 查询符合条件的最新数据行

Server configuration jupyter environment

Rider set the highlighted side of the selected word, remove the warning and suggest highlighting

IDEA 项目启动报错 Shorten the command line via JAR manifest or via a classpath file and rerun.

Design of electronic clock based on 51 single chip microcomputer

Winedt common shortcut key modify shortcut key latex compile button

十个顶级自动化和编排工具
随机推荐
Understand the usage of functions and methods in go language
Cartoon: a bloody case caused by a math problem
解决“双击pdf文件,弹出”请安装evernote程序
Knowledge points of MySQL (6)
力扣解法汇总729-我的日程安排表 I
哈趣K1和哈趣H1哪个性价比更高?谁更值得入手?
C#(Winform) 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件
Compter le temps d'exécution du programme PHP et définir le temps d'exécution maximum de PHP
华为云云原生容器综合竞争力,中国第一!
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
Accuracy of BigDecimal Division
How to save the trained neural network model (pytorch version)
Matery主题自定义(一)黑夜模式
Humi analysis: the integrated application of industrial Internet identity analysis and enterprise information system
独立开发,不失为程序员的一条出路
每日一练:关于日期的一系列
What are the precautions for MySQL group by
如何保存训练好的神经网络模型(pytorch版本)
Complete solution instance of Oracle shrink table space
ClickHouse(03)ClickHouse怎么安装和部署