1

I am trying to displey a few labels for exact amount of time and than forget them. I tried with sleep() and time.sleep(), but the program started after time I have defined and than executes lines. Here is part of my program:

from time import sleep
from tkinter import*
from tkinter import ttk
root = Tk()
root.geometry('700x700+400+100')
root.overrideredirect(1)
myFrame=Frame(root)
label1=Label(myFrame, text='Warning!', font=('Arial Black', '26'), fg='red')


myFrame.pack()
label1.pack()

sleep(10)



myFrame.pack_forget()
label1.pack_forget()

But when I run the program it wait for 10 seconds and than executes the lines (frame and label are packed and than immediatly forget).

I hope it is clear, what problem I have.

3
  • @DonkeyKong I want to display label1 for 10 seconds and than forget it. Commented Apr 6, 2015 at 17:44
  • And what is happening with your current code? Commented Apr 6, 2015 at 17:48
  • @DonkeyKong like I said: 10 sec it do nothing and than executes all the program I wrote (make root, set its geometry, etc.), but label1 is (I think so) packed (.pack) and than immediatly forget (.pack_forget()). Commented Apr 6, 2015 at 18:00

1 Answer 1

2

Use the Tkinter after method instead of time.sleep(), as time.sleep() should almost never be used in a GUI. after schedules a function to be called after a specified time in milliseconds. You could implement it like this:

myFrame.after(10000, myFrame.pack_forget)
label1.after(10000,label1.pack_forget)

Note that after does not ensure a function will occur at precisely the right time, it only schedules it to occur after a certain amount of time. As a result of Tkinter being single-threaded, if your app is busy there may be a delay measurable in microseconds (most likely).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.