我对tkinter很新,并且一直在尝试使用一些示例代码来更好地理解它。我正在尝试创建一个启动画面,对我打算运行的程序进行一些初始化。由于初始化可能需要一段时间,具体取决于网络连接,我正在尝试使用启动屏幕来提供状态,因为应用程序正在初始化..我的所有搜索都导致StringVar作为解决方案,但我迷路了为什么这种方法对我不起作用。我会在这方面得到一些帮助,并更好地了解该怎么做。
由于
from tkinter import *
from time import time, sleep, clock
class Splash:
def __init__(self, root, file, wait):
self.__root = root
self.__file = file
self.__wait = wait + clock()
def __enter__(self):
# Hide the root while it is built.
self.__root.withdraw()
# Create components of splash screen.
window = Toplevel(self.__root)
splash = PhotoImage(master=window, file=self.__file)
canvas = Label(window, text=d_status.get(), fg='white', bg='black', image=splash, compound=TOP)
# Get the screen's width and height.
scrW = window.winfo_screenwidth()
scrH = window.winfo_screenheight()
# Get the images's width and height.
imgW = splash.width()
imgH = splash.height()
# Compute positioning for splash screen.
Xpos = (scrW - imgW) // 2
Ypos = (scrH - imgH) // 2
# Configure the window showing the logo.
window.overrideredirect(True)
window.geometry('+{}+{}'.format(Xpos, Ypos))
canvas.grid()
# Show the splash screen on the monitor.
window.update()
# Save the variables for later cleanup.
self.__window = window
self.__canvas = canvas
self.__splash = splash
def __exit__(self, exc_type, exc_val, exc_tb):
# Ensure that required time has passed.
now = clock()
if now < self.__wait:
sleep(self.__wait - now)
# Free used resources in reverse order.
del self.__splash
self.__canvas.destroy()
self.__window.destroy()
# Give control back to the root program.
self.__root.update_idletasks()
self.__root.deiconify()
def initializeMyApplication():
for x in range(5):
d_status.set(x)
sleep(1)
print(d_status.get())
def buildTheGUI(object):
for x in range(100, 110):
sleep(1)
print(x)
root = Tk()
d_status = StringVar()
d_status.set("Initializing...")
with Splash(root, 'pix.gif', 3.0):
initializeMyApplication()
d_status.set("Done!")
buildTheGUI(root)
root.mainloop()