如何解决启动tkinter应用程序时,是否可以将资产作为空闲任务加载?
对不起,如果标题不是很清楚,但是这是我的问题:我正在尝试构建tkinter应用程序,但是当我启动它(无论是从命令行还是以.exe文件形式)时,您可以看到在很短的时间内,由tk.Tk()函数生成的空白窗口就会被放置到该窗口上。
但是我想做的是在用户看到资产之前将其加载到窗口中。如果我不太清楚,请原谅,当然,我愿意就我的问题作进一步的解释。
这是启动应用程序时使用的代码:
from classes.mainMenu import MainMenu
from classes.log import Log
from tkinter import *
import os
import json
import datetime
class GUI():
### Constructors Region
def __init__(self):
# Creation of the window
self.root = Tk()
self.width = 1080
self.height = 720
self.root.title("GeoDataProcessing")
self.root.iconbitmap('assets/logo.ico')
self.root.geometry(f'{self.width}x{self.height}+{int((self.root.winfo_screenwidth() - self.width) / 2)}+{- 50 + int((self.root.winfo_screenheight() - self.height) / 2)}')
self.root.minsize(1080,720)
self.root.config(bg = 'white')
# Style attributes
self.colorFont = '#3DC2F1'
self.bg = 'white'
self.buttonFont = ('Arial',13)
# Assets loading
self.background = 0
self.bannerBackground = 0
self.loadAssets()
# Protocols
self.root.protocol('WM_DELETE_WINDOW',self.closeApp)
# Others
self.currentDirectory = os.getcwd()
# display of the background
self.backgroundLabel = Label(self.root,image = self.background)
self.backgroundLabel.place(x = 0,y = 0,relwidth = 1,relheight = 1)
self.version = "0.3.1"
self.author = "Paul Wassermann"
self.authorLabel = Label(self.root,text = f"Author: {self.author}",font = ('Arial',12),fg = self.colorFont,bg = self.bg,bd = 2,relief = RIDGE)
self.authorLabel.place(relx = 0.02,rely = 0.95)
self.versionLabel = Label(self.root,text = f"Version {self.version}",relief = RIDGE)
self.versionLabel.place(relx = 0.98,rely = 0.95,anchor = NE)
self.mainMenu = MainMenu(self)
self.display()
### Methods Region
def loadAssets(self):
try:
self.background = PhotoImage(file = "assets/background.png")
self.bannerBackground = PhotoImage(file = "assets/bannerBackground.png")
except:
Log().writeLogException(level = "error",message = "Un problème est survenu lors du chargement des éléments visuels.\nConsultez le log pour plus d'informations.")
def display(self):
try:
# Creation of the banner
self.bannerFrame = Frame(self.root,width = 1080,height = 720)
# Creation of the canvas containing the banner background as well as the text banner
self.bannerCanvas = Canvas(self.bannerFrame,width = 840,height = 420,relief = RIDGE)
self.bannerCanvas.create_image(int(self.bannerCanvas.cget("width")) / 2,100 + int(self.bannerCanvas.cget("height")) / 2,anchor = CENTER,image = self.bannerBackground)
self.bannerCanvas.create_text(int(self.bannerCanvas.cget("width")) / 2,- 40 + int(self.bannerCanvas.cget("height")) / 2,text = "Bienvenue sur GeoDataProcessing!",font = ("Arial Bold",32),fill = self.bg)
self.bannerCanvas.create_text(int(self.bannerCanvas.cget("width")) / 2,40 + int(self.bannerCanvas.cget("height")) / 2,text = "L'outil de traitement des données d'essais pressiométriques",20),fill = self.bg)
self.bannerCanvas.pack()
# Creation of the button to launch the application
self.startButton = Button(self.root,text = "Commencer",15),relief = RAISED,activeforeground = self.colorFont,activebackground = self.bg,command = lambda: [self.hide(),self.mainMenu.display()])
self.root.bind("<Return>",lambda e: [self.hide(),self.mainMenu.display()])
self.startButton.pack(side = BottOM,pady = 50)
# display of the banner
self.bannerFrame.pack(expand = 'yes')
except:
Log().writeLogException()
def hide(self):
try:
self.bannerFrame.destroy()
self.startButton.destroy()
self.root.unbind("<Return>")
except:
Log().writeLogException()
def closeApp(self):
try:
dump = {}
for key in self.mainMenu.configurationMenu.parameters:
if key == "Last connection":
dump[key] = f"{datetime.date.today()}"
else:
dump[key] = self.mainMenu.configurationMenu.parameters[key].get()
with open(f"{self.currentDirectory}/files/paramètres.txt",'w') as file:
json.dump(dump,file,sort_keys = True,indent = 4)
except:
Log().writeLogException()
finally:
self.root.quit()
如果您想知道为什么我将图像加载到单独的函数中而不是在构造函数中加载,则是为了将其放在try..except
块中。
编辑: 实际启动应用程序的代码(在我的main.py文件中):
from classes.tubeCalibration import TubeCalibration
from classes.airCalibration import AirCalibration
from classes.gui import GUI
from classes.log import Log
try:
gui = GUI()
gui.root.mainloop()
except:
Log().writeLogException(level = "error",message = "Un problème est survenu lors de l'exécution de l'application.\nContactez le support ou consultez le log détaillé pour résoudre le problème.")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。