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

查看:159
本文介绍了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的东西

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

其他stackoverflow问题.

这是使用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天全站免登陆