python - Tkinter 显示初始屏幕并隐藏主屏幕,直到 __init__ 完成

标签 python tkinter splash-screen

我有一个 tkinter 主窗口,可能需要几秒钟才能正确加载。因此,我希望在主类的 init 方法完成之前显示启动画面,并且可以显示主 tkinter 应用程序。如何实现?

启动画面代码:

from Tkinter import *
from PIL import Image, ImageTk
import ttk

class DemoSplashScreen: 
    def __init__(self, parent): 
        self.parent = parent 
        self.aturSplash() 
        self.aturWindow() 

    def aturSplash(self): 
        self.gambar = Image.open('../output5.png')
        self.imgSplash = ImageTk.PhotoImage(self.gambar)

    def aturWindow(self):
        lebar, tinggi = self.gambar.size 
        setengahLebar = (self.parent.winfo_screenwidth()-lebar)//2 
        setengahTinggi = (self.parent.winfo_screenheight()-tinggi)//2
        self.parent.geometry("%ix%i+%i+%i" %(lebar, tinggi, setengahLebar,setengahTinggi))
        Label(self.parent, image=self.imgSplash).pack()


if __name__ == '__main__': 
    root = Tk()
    root.overrideredirect(True) 
    progressbar = ttk.Progressbar(orient=HORIZONTAL, length=10000, mode='determinate') 
    progressbar.pack(side="bottom") 
    app = DemoSplashScreen(root) 
    progressbar.start()
    root.after(6010, root.destroy) 
    root.mainloop()

主 tkinter 窗口最小工作示例:

import tkinter as tk

root = tk.Tk()

class Controller(tk.Frame):
    def __init__(self, parent):
        '''Initialises basic variables and GUI elements.'''
        frame = tk.Frame.__init__(self, parent,relief=tk.GROOVE,width=100,height=100,bd=1)

control = Controller(root)
control.pack()
root.mainloop()

编辑:我可以使用主窗口,直到它使用 .withdraw() 和 .deiconify() 方法完成加载。但是我的问题是我找不到在这两个方法调用之间运行启动画面的方法。

最佳答案

一个python3的简单例子:

#!python3

import tkinter as tk
import time

class Splash(tk.Toplevel):
    def __init__(self, parent):
        tk.Toplevel.__init__(self, parent)
        self.title("Splash")

        ## required to make window show before the program gets to the mainloop
        self.update()

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.withdraw()
        splash = Splash(self)

        ## setup stuff goes here
        self.title("Main Window")
        ## simulate a delay while loading
        time.sleep(6)

        ## finished loading so destroy splash
        splash.destroy()

        ## show window again
        self.deiconify()

if __name__ == "__main__":
    app = App()
    app.mainloop()

这样的事情在 tkinter 中很困难的原因之一是窗口仅在程序未运行特定功能时才更新,因此到达主循环。对于像这样的简单事情,您可以使用 updateupdate_idletasks 命令使其显示/更新,但是如果延迟时间过长,则在 Windows 上窗口可能会变得“无响应” "

解决此问题的一种方法是在整个加载例程中放置多个 updateupdate_idletasks 命令,或者使用线程。

但是,如果您使用线程,我建议您不要将 splash 放入它自己的线程(可能更容易实现),您最好将加载任务放入它自己的线程,保持工作线程和 GUI 线程分开,因为这往往会提供更流畅的用户体验。

关于python - Tkinter 显示初始屏幕并隐藏主屏幕,直到 __init__ 完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38676617/

相关文章:

windows-phone-7 - 如何在 WP7 启动画面和具有相同启动画面的第一页之间进行无缝转换

Android Activity 背景图片

python - Selenium Firefox Webdriver 抛出 URLError

python - 如何检查文本文件中的一行文本?

python - Matplotlib 表面颜色不纯

python - IntVar().trace() 不起作用

python - 如何使用 Tkinter 并让我的应用程序保持焦点?

python - 使用Selenium解决无限滚动问题

python - 使用 Python Tkinter : Always on top window isn't showing custom class tooltip text

iphone - 如何在显示启动画面时从 Web 服务加载数据?