Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

python - 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')
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...