Ben L
Ben L

Reputation: 783

Open Tkinter multiple Windows with delay (Python)

I've been trying to open several Tkinter windows with a delay of couple seconds between each Tkinter window.

My Script so far :

import Tkinter as tk
import os
from PIL import Image, ImageTk
from win32api import GetSystemMetrics
from time import sleep
from random import uniform

root = tk.Tk()
tk.Label(root, text="this is the root window").pack()
root.geometry("10x10")
l = []

def showPic(i):
    if(i<5):
        loc = os.getcwd() + '\pictures\pic%s.jpg' % i
        img = Image.open(loc)
        img.load()
        photoImg = ImageTk.PhotoImage(img)
        l.append(photoImg)
        window = tk.Toplevel()
        window.geometry("750x750+%d+%d" % (uniform(0, GetSystemMetrics(0) - 750),     uniform(0, GetSystemMetrics(1) - 750)))
        tk.Label(window, text="this is window %s" % i).pack()
        tk.Label(window, image=photoImg).pack()
        root.after(1000, showPic(i+1))
    else:
        return


root.after(0, showPic(1))
root.mainloop()

I also tried using the sleep() and after() functions with no success. It waits the time before preparing the window, but shows them all together, simultaniously, after the time I set

Upvotes: 1

Views: 595

Answers (1)

furas
furas

Reputation: 142641

after expects function name without () and arguments

Using

root.after(1000, showPic(i+1))

is like

result = showPic(i+1)
root.after(1000, result)

You can use lambda

root.after(1000, lambda:showPic(i+1))

because it is almost like

def new_function():
    showPic(i+1)

root.after(1000, new_function)

or

root.after(1000, showPic, i+1)

Upvotes: 2

Related Questions