문제

I'm relatively new to Python and Tkinter and I am striving to get my head over how mainloop and the after method work. More specifically, I want to create a splashScreen, which goes away after a timeframe, and then the actual mainWindow is shown.

My code looks like this:

class SplashScreen:
    # some code here

    def destroyMe(self):
        self.destroy()

    def destroySplashScreen(self, timeToSleep=0):
        if timeToSleep > 0:
            self.master.after(timeToSleep*1000, self.destroyMe())

    # some other code here

if __name__ == '__main__':
    root = Toplevel()
    mySP = SplashScreen(root)
    mySP.populateSplashScreen()
    mySP.destroySplashScreen(5)
    root.mainloop()

However, what the code does is to create the whole window after the timeframe given (5 sec.) without any content. Instead, it should create it, wait 5 sec. and then destroy it.

도움이 되었습니까?

해결책

Working example

after expects only function name (without ()).

destroy() needs self.master

from Tkinter import *

class SplashScreen:

    # some code here

    def __init__(self, master):
        self.master = master
        self.master.title("SplashScreen")

    def destroyMe(self):
        self.master.destroy()

    def destroySplashScreen(self, timeToSleep=0):
        if timeToSleep > 0:
            self.master.after(timeToSleep*1000, self.destroyMe)

    # some other code here

if __name__ == '__main__':
    root = Toplevel()
    mySP = SplashScreen(root)
    #mySP.populateSplashScreen()
    mySP.destroySplashScreen(3)
    root.mainloop()

BTW: Toplevel is used to create "child" window so (in example) it create automaticly "parent" window - so I add title("SplashScreen")

BTW: if you will use command= in widget - it also expects function name without ().

If you use (in command or after) function with () you run that function and result is assigned to command= or use as parameter for after.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top