Reputation: 23
I am trying to recreate the Pokemon red fight scene and I need a 5 second delay between the first character appearance (the good guy) and the second (the bad guy). I have tried the time.sleep() method however it just waits 5 seconds (sleep time) before launching the program.
#Add Person
label = Label(image=diglett, bg= "white")
label.place(x=20, y=80)
#Add Enemy (I want a 5 second delay here...)
label2 = Label(image=bad_guy, bg= "white")
label2.place(x=200, y=20)
label2.after(100)
bad_guy_name = Label(root, text="Cabbage", bg='white')
bad_guy_name.place(x=35, y=30)
I have seen another Stack Overflow post about this however I am not sure how to implement this into my code: Time delay Tkinter
Thanks
Upvotes: 1
Views: 3276
Reputation: 386352
You would create a function to add the label or change the label, and then request that it be run in the future with after
:
First, create the function:
def showLabel():
label = Label(root, ...)
label.place()
Next, call it from your main code via after
:
#Add Enemy (I want a 5 second delay here...)
root.after(5000, showLabel)
That directly answers your question, but you have several other issues. One, if you call pack
and then call place
on the same widget, pack
has no effect. A widget can only be managed by one of pack
, place
or grid
, and the last one you use "wins".
If you are using place
, placing something in five seconds should work more-or-less as you expect. If you use pack
, bear in mind that it might cause your window to resize, and without any options it will appear at the bottom of it's parent window.
Finally, depending on what you're actually trying to do, you might want to consider creating and packing the label immediately, but having it be blank. Then, after five seconds you can change the text from a blank string to something else.
Upvotes: 3