当前位置:网站首页>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创新
边栏推荐
- How to modify MySQL fields as self growing fields
- Vulnerability recurrence - 48. Command injection in airflow DAG (cve-2020-11978)
- SQL Server(2)
- 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. Scala class
- Server configuration jupyter environment
- Flask solves the problem of CORS err
- 統計php程序運行時間及設置PHP最長運行時間
- Why is February 28 in the Gregorian calendar
- Domain name resolution, reverse domain name resolution nbtstat
猜你喜欢

mongodb(快速上手)(一)

Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition

Abnormal recovery of virtual machine Oracle -- Xi Fenfei

Machine learning 01: Introduction

Short the command line via jar manifest or via a classpath file and rerun

Knowledge points of MySQL (7)

基于51单片机的电子时钟设计

Machine learning 02: model evaluation

服务器配置 jupyter环境

Vulnerability recurrence - 48. Command injection in airflow DAG (cve-2020-11978)
随机推荐
这个17岁的黑客天才,破解了第一代iPhone!
Seven Devops practices to improve application performance
漫画:如何实现大整数相乘?(上) 修订版
mongodb(快速上手)(一)
网络威胁分析师应该具备的十种能力
Is it safe for China Galaxy Securities to open an account? How long can I buy stocks after opening an account
服务器配置 jupyter环境
Accuracy of BigDecimal Division
一口气读懂 IT发展史
BigDecimal除法的精度问题
33: Chapter 3: develop pass service: 16: use redis to cache user information; (to reduce the pressure on the database)
Force deduction solution summary 729- my schedule I
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
力扣解法汇总729-我的日程安排表 I
Matery主题自定义(一)黑夜模式
关于mysql中的json解析函数JSON_EXTRACT
Cartoon: how to multiply large integers? (integrated version)
thinkphp3.2.3
北京内推 | 微软亚洲研究院机器学习组招聘NLP/语音合成等方向全职研究员
Disabling and enabling inspections pycharm