0

I am writing an application using the tkinter module in Python. The section of code I am having trouble with is:

def sceneChange():    
    global num
    num = num + 1
    currentScene = scenes[num]
    global label
    label.destroy()
    label = tk.Label(window, image = currentScene)
    label.pack()
    time1 = dt.datetime.utcnow().minute + dt.datetime.utcnow().second / 60 
    testTime = time1 + 4.44 / 60 # ~5 secs in the future
    while dt.datetime.utcnow().minute + dt.datetime.utcnow().second / 60 < testTime: 
        pass
    label.destroy()
    num = num + 1
    currentScene = scenes[num]
    label = tk.Label(window, image = currentScene)
    label.pack()

Which is used in:

b = tk.Button(label, text = "Start", command = sceneChange, height = 1, width = 10)
b.place(x = 440, y = 48)

The label.pack command before the while loop is not showing in my window. I am trying to make it display for 5 seconds then switch the image to something else. But, it isn't. All help would be appreciated. If I have done something wrong in the formatting of my question, please let me know so I can improve it :). The whole code is below:

import tkinter as tk
import datetime as dt

window = tk.Tk()
window.title("Game")
scenes = [tk.PhotoImage(file = "TitleScreen.gif"), tk.PhotoImage(file = "ControlsScreen.gif"), tk.PhotoImage(file = "game.gif")]
num = 0
currentScene = scenes[num]
label = tk.Label(window, image = currentScene)


def sceneChange():
    global num
    num = num + 1
    currentScene = scenes[num]
    global label
    label.destroy()
    label = tk.Label(window, image = currentScene)
    label.pack()
    time1 = dt.datetime.utcnow().minute + dt.datetime.utcnow().second / 60 # current time
    testTime = time1 + 4.44 / 60 # ~5 secs in the future
    while dt.datetime.utcnow().minute + dt.datetime.utcnow().second / 60 < testTime: 
        pass
    label.destroy()
    num = num + 1
    currentScene = scenes[num]
    label = tk.Label(window, image = currentScene)
    label.pack()


label.pack()
b = tk.Button(label, text = "Start", command = sceneChange, height = 1, width = 10)
b.place(x = 440, y = 48)
b1 = tk.Button(label, text = "Quit", command = exit, height = 1, width = 10)
b1.place(x = 440, y = 78)

label.mainloop()

Jake
  • 7
  • 5
  • Jake you have to `after()` instead of while loop, if you use while loop it'll hangs till the loop is done so the first image won't show up anyway. See this post https://stackoverflow.com/questions/25753632/tkinter-how-to-use-after-method – Saad Apr 19 '19 at 08:24
  • @Saad Could you please put your comment as an answer so I can accept it, as it did work – Jake Apr 19 '19 at 09:48

1 Answers1

0

You should use after() function instead of while loop because using while loop causes the window to freeze until the loop ends.

If you want to know how to use after()

Go to this post


Now, How you can achieve a picture to display for 5 sec?

Here is an example.

from tkinter import *

root = Tk()

root.geometry("250x250")

Img1 = PhotoImage(file="img1.png")     # Image 1
Img2 = PhotoImage(file="img2.png")      # Image 2

L = Label(root, image=Img1)
L.pack()

# The image will change in 5000ms ( 5secs )
root.after( 5000, lambda: L.config(image=Img2) )

root.mainloop()
Saad
  • 3,340
  • 2
  • 10
  • 32