Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tkinter and time.sleep

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()
like image 981
Brandon Nadeau Avatar asked May 01 '12 05:05

Brandon Nadeau


1 Answers

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

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

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()
like image 56
srgerg Avatar answered Sep 25 '22 13:09

srgerg