Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time delay Tkinter

I'd like to make a graphic window in PyDev (Eclipse) python 2.75.

I've done few things but I'd like to make an "entrance" "blink". It's Tests the user input. If it's an integer it should blink green for a second, and then turn into white. But if it's a string of something else it should blink red, and then turn into white. I've used a time.sleep() but it doesn't work as I'd like to.

Here is my code for this action:

def sprawdzam():

    z = e.get()
    try:
        z = int(z)
        e.config(bg = 'green')
        time.sleep(2)
        e.config(bg = 'white')    

    except:
        l.config(bg = 'red')
        time.sleep(2)
        e.config(bg = 'white')
like image 434
Maq92 Avatar asked Nov 10 '13 08:11

Maq92


People also ask

How do I make Python wait before printing?

If you've got a Python program and you want to make it wait, you can use a simple function like this one: time. sleep(x) where x is the number of seconds that you want your program to wait.

What is after in Python?

The after() method calls the callback function once after a delay milliseconds (ms) within Tkinter's main loop. If you don't provide the callback , the after() method behaves like the time. sleep() function. However, the after() method uses the millisecond instead of the second as the unit.


1 Answers

time.sleep blocks the execution of the program.

Use after.

For example:

from Tkinter import *

def blink():
    e.config(bg='green')
    e.after(1000, lambda: e.config(bg='white')) # after 1000ms

root = Tk()
e = Entry(root)
e.pack()
b = Button(root, text='blink', command=blink)
b.pack()
root.mainloop()
like image 53
falsetru Avatar answered Sep 30 '22 13:09

falsetru