I wish to create a loop which adds images across a screen with random positions:

photo1 = PhotoImage(file=address) #Assign image to PhotoImage
    XValue = randint(0,widthOfScreen-width2) #width2 = image width
    YValue = randint(0,heightOfScreen-height2) #width2 = image height
    Label(mainGUI, background = "white", image = photo1, borderwidth = 0).place(x=XValue, y = YValue)

the code works and places the objects randomly but if i try and add a

time.sleep(1)

into the loop then the gui isnt made until the ned of the program with all images finished....

Recommended Answers

All 5 Replies

I'm not sure if they can function well together

Don't use sleep() in gui programs. Use a timer instead (method after() in tkinter). Run this example by PyTony to see how it works.

Actually you can use time.sleep() with Tkinter but you have to use update() too ...

''' tk_image_slideshow_sleep.py
create a very simple Tkinter image slide show
using time.sleep() and root.update()
tested with Python27/33  by  vegaseat  03dec2013
'''

import time
try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

# get a series of gif images you have in the working folder
# or use full path name
image_files = [
'Slide_Farm.gif',
'Slide_House.gif',
'Slide_Sunset.gif',
'Slide_Pond.gif',
'Slide_Python.gif'
]

root = tk.Tk()
label = tk.Label(root)
label.pack()

delay = 3.2  # seconds delay between slides
for image in image_files:
    image_object = tk.PhotoImage(file=image)
    label.config(image=image_object)
    root.title(image)
    root.update()
    time.sleep(delay)

root.mainloop()
commented: Very helpful +1
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.