0

In my code, I am trying to make a loading screen for a frogger game but for some reason I am encountering a problem where I display a picture and then do the .sleep function before displaying a label over the top of it however it displays both of them at the same time it just runs the code 1 second after it should, can anyone help?

Here is my code below:

from tkinter import *

import tkinter as tk

import time

window = Tk()
window.geometry("1300x899")

LoadingScreen = PhotoImage(file = "FroggerLoad.gif")

Loading = Label(master = window, image = LoadingScreen)

Loading.pack()

Loading.place(x = 65, y = 0)

time.sleep(1)

FroggerDisplay = Label(master = window, font ("ComicSans",100,"bold"),text = "Frogger")
FroggerDisplay.pack()

FroggerDisplay.place(x = 500, y = 300)

window.mainloop()
TrebledJ
  • 8,713
  • 7
  • 26
  • 48

1 Answers1

0

When you use time.sleep(1) before starting the window.mainloop(), the window is created only after 1 second, and the FroggerDisplay label will be created at the same time with it. So, you can't use time.sleep(seconds) now.

However, you can use window.after(ms, func) method, and place into the function all the code between time.sleep(1) and window.mainloop(). Note, that unlike the time.sleep(seconds) you must give the time to window.after (the first argument) as milliseconds.

Here is the edited code:

from tkinter import *


def create_fd_label():
    frogger_display = Label(root, font=("ComicSans", 100, "bold"), text="Frogger")  # create a label to display
    frogger_display.place(x=500, y=300)  # place the label for frogger display

root = Tk()  # create the root window
root.geometry("1300x899")  # set the root window's size

loading_screen = PhotoImage(file="FroggerLoad.gif")  # create the "Loading" image
loading = Label(root, image=loading_screen)  # create the label with the "Loading" image
loading.place(x=65, y=0)  # place the label for loading screen

root.after(1000, create_fd_label)  # root.after(ms, func)
root.mainloop()  # start the root window's mainloop

PS: 1) Why do you use .pack(...) and then .place(...) methods at the same time - the first one (.pack(...) here) will be ignored by Tkinter.
2) It's better to use a Canvas widget for creating a game - unlike labels it supports transparency and simpler to use. For example:

from tkinter import *


root = Tk()  # create the root window
root.geometry("1300x899")  # set the root window's size
canv = Canvas(root)  # create the Canvas widget
canv.pack(fill=BOTH, expand=YES) # and pack it on the screen

loading_screen = PhotoImage(file="FroggerLoad.gif")  # open the "Loading" image
canv.create_image((65, 0), image=loading_screen)  # create it on the Canvas

root.after(1000, lambda: canv.create_text((500, 300),
                                          font=("ComicSans", 100, "bold"),
                                          text="Frogger"))  # root.after(ms, func)
root.mainloop()  # start the root window's mainloop

Note: you might need to change coords with Canvas.

Demian Wolf
  • 1,698
  • 2
  • 14
  • 34