gpt4 book ai didi

python - 时间延迟 Tkinter

转载 作者:太空狗 更新时间:2023-10-29 22:30:46 24 4
gpt4 key购买 nike

我想在 PyDev (Eclipse) python 2.75 中创建一个图形窗口。

我做了一些事情,但我想做一个“入口”“眨眼”。它测试用户输入。如果它是一个整数,它应该闪烁绿色一秒钟,然后变成白色。但如果它是一串其他东西,它应该闪烁红色,然后变成白色。我使用了 time.sleep(),但它无法正常工作。

这是我执行此操作的代码:

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

最佳答案

time.sleep 阻止程序的执行。

使用after .

例如:

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

关于python - 时间延迟 Tkinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19887729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com