Home > front end >  Displaying Multiple image in tkinter with delay
Displaying Multiple image in tkinter with delay

Time:03-16

I want to display one image at a time this is the code i have written for 2 image. But it does not work as i want to. I want that after the 1st has completely finished then it takes 10 sec delay then run the second ad.

def ad():
    global pop
    pop=Toplevel(root)
    pop.geometry('1600x900')
    global pic

    pic=PhotoImage(file='ad.png')

    label_pop = Label(pop, image=pic)
    label_pop.grid()

    pop.overrideredirect(1)
    pop.after(5000, lambda: pop.destroy())


    
    #ad 2
    global pop2
    pop2=Toplevel(root)
    pop2.geometry('1600x900')
    global pic2

    pic2=PhotoImage(file='j..png')

    label_pop2 = Label(pop2, image=pic2)
    label_pop2.grid()

    pop2.overrideredirect(1)
    pop2.after(5000, lambda: pop2.destroy())
    root.after(15000, ad)




root.after(5000,ad)

CodePudding user response:

Create a cycle list of the images you want to show using itertools.cycle() and then you can get the next image using next().

Below is an example:

...
from itertools import cycle

...

# a cycle list of images you want to show
imagelist = cycle(['ad1.png', 'ad2.png', ...])

def ad():
    pop = Toplevel(root)
    pop.geometry('1600x900')
    pop.overrideredirect(1)

    pic = PhotoImage(file=next(imagelist)) # get next image using next()

    label_pop = Label(pop, image=pic)
    label_pop.grid()
    label_pop.image = pic  # save reference of image to avoid garbage collection

    root.after(5000, pop.destroy)
    root.after(15000, ad)

root.after(5000, ad)
...

CodePudding user response:

  1. You need to use different file names for each image.
  2. You can use the time module and specifically time.delay() for the delay.
  • Related