Tkininter
Tkininter

Reputation: 65

Python 3.4 tkinter sleep

I want to show all products within

itertools.product(string.punctuation, repeat=2))

from index 0 to end with sleep 0.5 sec after each element. Unfortunately tkinter hasn't normal sleep function. I can use only

w.after(time, callback)

but this is bad for what I want to do.

I want to do something like this:

t=0
for i in k:
    e1.delete(0,END)
    e1.insert(0,u[t])
    time.sleep(0.5)
    t+=1

It's my all code with first 3 products (I tried used functions for this, but too many functions are needed to show all products). Anyone have idea what should I do to make it work?

from tkinter import *
import itertools
import string

def d():
    e1.delete(0,END)
    e1.insert(0,u[1])

def y():
    e1.delete(0,END)
    e1.insert(0,u[2])

root = Tk()
l1 = Label(root, text="Current product:")
l1.pack(side = LEFT)
e1 = Entry(root)
e1.pack(side = RIGHT)

k = list(''.join(c) for c in itertools.product(string.punctuation, repeat=2))
e=" "
u = e.join(k)
u = u.split(e)  
e1.insert(0,u[0])
e1.after(1000,d)
e1.after(2000,y)

root.mainloop()

Upvotes: 0

Views: 194

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386362

You can pass arguments via after:

def callback(product):
    e1.delete(0,END)
    e1.insert(0,product)

e1.after(1000,callback, u[1])
e1.after(2000,callback, u[2])

You can also do that in a loop:

for seconds, product in enumerate(k):
    e1.after(seconds*1000, callback, product)

Upvotes: 2

Related Questions