2

The problem I've encountered is while I am attempting to keep the first functions(StartUpScr) widgets on screen for 3 seconds using time.sleep(3) before deleting all it's widgets placed on screen and then continuing to place the next functions(MenuScr) widgets. I've managed to successfully use destroy() to delete the first widgets and replace them with the second widgets, but for some reason when inputting time.sleep(3) anywhere in the functions and the main program, rather than the first widgets staying for 3 seconds and then being replaced it delays the start of the program producing a blank screen before quickly flashing past the first screen onto the second.

from tkinter import *
import tkinter
import time



window = tkinter.Tk()                               
window.title("BINARY-SUMS!!!")                      
window.geometry("1000x800")                        
window.wm_iconbitmap('flower3.ico')                 
window.configure(background='lavender')            

def StartUpScr():
    StartUpScr = tkinter.Label(window, text="FIRST-SCREEN!!!",fg = "Aqua",bg = "Lavender",font = ("Adobe Gothic Std B", 90, "bold" )).pack()

StartUpLabel = tkinter.Label(window, text="Developed by Robert Bibb 2016",bg = "Lavender",font = ("Calibri Light (Headings)", 10, "italic" ))
    StartUpLabel.pack()
    StartUpLabel.place(x = 400, y = 775)


def MenuScr():
    StartUpScr = tkinter.Label(window, text="SECOND-SCREEN!!!",fg = "green",bg = "Lavender",font = ("Adobe Gothic Std B", 85, "bold" ))
    StartUpScr.pack()


if __name__ == "__main__":
    StartUpScr()
    time.sleep(3)
    for widget in window.winfo_children():
        widget.destroy()
    MenuScr()
Rob Bibb
  • 39
  • 6
  • 1
    You're looking for [`window.after(time, callback)`](http://stackoverflow.com/questions/25753632/tkinter-how-to-use-after-method#25753719) – Aran-Fey Jul 23 '16 at 18:47
  • I would use different names for a function and a variable. – Parviz Karimli Jul 23 '16 at 21:20
  • Hi Robb, would you give feedback to the below answerers, please? They both look like they made a good effort in trying to help you. – halfer Sep 23 '16 at 08:43

2 Answers2

2

time.sleep() won't work here as it stops the execution of the program, you'll have to use after...and its also a bad practice to use sleep in GUI programming.

root.after(time_delay, function_to_run, args_of_fun_to_run)

So in your case it'll like

def destroy():
    #destroy here
    for widget in window.winfo_children():
        widget.destroy()

and after the if statement -

if __name__ == "__main__":
    StartUpScr()
    window.after(3000, destroy)
    MenuScr()
hashcode55
  • 5,622
  • 4
  • 27
  • 40
  • This isn't quite right -- `MenuScr()` is going to run immediately. You should create a function that both destroys the original screen and creates the new screen, or use `after` to call `MenuScr()` in 3001 milliseconds. – Bryan Oakley Jul 23 '16 at 20:25
1

So we are defining three functions: firstScreen, secondScreen, and changeScreen. The idea is running firstScreen and after 3 seconds, running changeScreen which will destroy the current parent window (master) and create the next brand new parent window (master2) that will call secondScreen which has completely new world. This is how it's going to happen:

from tkinter import *
root = Tk()
import time

class App:
    def __init__(self, master):
        self.master = master
        self.master.geometry("500x500-500+50")

    def firstScreen(self):
        self.master.title("FIRST SCREEN")
        self.label1 = Label(self.master, width=50, height=20,
                            text="This is my FIRST screen", bg='red')
        self.label1.pack()
        self.master.after(3000, self.changeScreen)

    def secondScreen(self):
        self.label2 = Label(self.master, width=50, height=20,
                            text="This is my SECOND screen", bg='yellow')
        self.label2.pack()

    def changeScreen(self):
        self.master.destroy()
        self.master2 = Tk()
        self.master2.title('SECOND SCREEN')
        myapp = App(self.master2)
        myapp.secondScreen()

myapp = App(root)
myapp.firstScreen()

I hope it helps!

Parviz Karimli
  • 1,247
  • 1
  • 14
  • 26
  • Why destroy and recreate the root window? Why not just create one root window, and make each screen a frame? While I suppose it's harmless to create and destroy multiple root windows, tkinter is designed to have a single root window that lives for the life of the program. – Bryan Oakley Jul 23 '16 at 23:01
  • @BryanOakley You're right, and you're welcome to edit my code. I actually wanted to do it using frames. But I just choosed this way based on the body of the question: "I've managed to successfully use destroy() to delete the first widgets and replace them with the second widgets". So I thought the problem is there. It would be great to see a feedback from the user who asked the question. – Parviz Karimli Jul 23 '16 at 23:16