我试图在等待 5 秒后删除文本框中的文本,但程序不会运行,并且会休眠其他所有内容。还有一种方法可以让我的文本框休眠,这样我就可以在文本冻结时运行其他代码?
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 这样的东西后方法 http://infohost.nmt.edu/tcc/help/pubs/tkinter/universal.html而不是time.sleep(...)
.
这里有一个使用 after 方法的示例其他 stackoverflow 问题 https://stackoverflow.com/questions/2400262/code-a-timer-in-a-python-gui-in-tkinter.
这是使用 after 方法的脚本的修改版本:
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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)