Tkinter 显示闪屏并隐藏主屏幕,直到 __init__ 完成 [英] Tkinter Show splash screen and hide main screen until __init__ has finished

查看:23
本文介绍了Tkinter 显示闪屏并隐藏主屏幕,直到 __init__ 完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a main tkinter window that can take up to a few seconds to load properly. Because of this, I wish to have a splash screen that shows until the init method of the main class has finished, and the main tkinter application can be shown. How can this be achieved?

启动画面代码:

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 窗口最小工作示例:

Main tkinter window minimum working example:

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() 方法完成加载.但是我的问题是我找不到让启动画面在这两个方法调用之间运行的方法.

I can use the main window until it has finished loading using the .withdraw() and .deiconify() methods. However my problem is that I cannot find a way to have the splash screen running in the period between these two method calls.

推荐答案

python3 的一个简单示例:

a simple example for 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 上窗口可能会变得无响应""

one of the reasons things like this are difficult in tkinter is that windows are only updated when the program isn't running particular functions and so reaches the mainloop. for simple things like this you can use the update or update_idletasks commands to make it show/update, however if the delay is too long then on windows the window can become "unresponsive"

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

one way around this is to put multiple update or update_idletasks command throughout your loading routine, or alternatively use threading.

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

however if you use threading i would suggest that instead of putting the splash into its own thread (probably easier to implement) you would be better served putting the loading tasks into its own thread, keeping worker threads and GUI threads separate, as this tends to give a smoother user experience.

这篇关于Tkinter 显示闪屏并隐藏主屏幕,直到 __init__ 完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆