tkinter 和 time.sleep [英] tkinter and time.sleep

查看:47
本文介绍了tkinter 和 time.sleep的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在等待 5 秒后删除文本框中的文本,但该程序不会运行并且会休眠其他所有内容.还有一种方法可以让我的文本框休眠,这样我就可以在文本冻结时运行其他代码吗?

I am trying to delete text inside a text box after waiting 5 seconds, but instead the program wont run and does sleep over everything else. Also is there a way for me to just make my textbox sleep so i can run other code while the text is frozen?

from time import time, sleep
from Tkinter import *

def empty_textbox():
    textbox.insert(END, 'This is a test')
    sleep(5)
    textbox.delete("1.0", END)

root = Tk()

frame = Frame(root, width=300, height=100)
textbox = Text(frame)

frame.pack_propagate(0)
frame.pack()
textbox.pack()

empty_textbox()

root.mainloop()

推荐答案

你真的应该使用类似 Tkinter 的东西 after 方法 而不是 time.sleep(...).

You really should be using something like the Tkinter after method rather than time.sleep(...).

这里有一个使用 after 方法的例子 otherstackoverflow 问题.

There's an example of using the after method at this other stackoverflow question.

这是使用 after 方法的脚本的修改版本:

Here's a modified version of your script that uses the after method:

from time import time, sleep
from Tkinter import *

def empty_textbox():
    textbox.delete("1.0", END)

root = Tk()

frame = Frame(root, width=300, height=100)
textbox = Text(frame)

frame.pack_propagate(0)
frame.pack()
textbox.pack()

textbox.insert(END, 'This is a test')
textbox.after(5000, empty_textbox)

root.mainloop()

这篇关于tkinter 和 time.sleep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆