I got this code on using splash screens and liked it however, I'm unable to get the text to update based on the current event(in the example it's just a range of numbers). Would love to get some input on what I'm doing wrong here.
Thank you

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()

Recommended Answers

All 2 Replies

This works for me

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, textvariable=d_status, 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) // 3
        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
        return self

    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 foo(self, x):
        d_status.set(str(x))
        self.__root.update_idletasks()

def initializeMyApplication(notify_status):
    for x in range(5):
        notify_status(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) as s:
   initializeMyApplication(s.foo)
   s.foo("Done!")
   buildTheGUI(root)
root.mainloop()

Thank you! It makes sense to me looking at the additions you made

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.