How do I add a time delay to a tkinter Label?

Question:

I’m creating a Treasure Island type game with tkinter and I’m trying to add a delay before the label "line1" appears in the GUI, I’ve tried the after function but I don’t know how I would properly implement it. Anyway here’s the code:

import time
import tkinter
window = tkinter.Tk()

window.title("Treasure Island")
window.geometry("500x500")

logo = tkinter.Label(window, text='''
          |                   |                  |                     |
 _________|________________.=""_;=.______________|_____________________|_______
|                   |  ,-"_,=""     `"=.|                  |
|___________________|__"=._o`"-._        `"=.______________|___________________
          |                `"=._o`"=._      _`"=._                     |
 _________|_____________________:=._o "=._."_.-="'"=.__________________|_______
|                   |    __.--" , ; `"=._o." ,-"""-._ ".   |
|___________________|_._"  ,. .` ` `` ,  `"-._"-._   ". '__|___________________
          |           |o`"=._` , "` `; .". ,  "-._"-._; ;              |
 _________|___________| ;`-.o`"=._; ." ` '`."` . "-._ /_______________|_______
|                   | |o;    `"-.o`"=._``  '` " ,__.--o;   |
|___________________|_| ;     (#) `-.o `"=.`_.--"_o.-; ;___|___________________
____/______/______/___|o;._    "      `".o|o_.--"    ;o;____/______/______/____
/______/______/______/_"=._o--._        ; | ;        ; ;/______/______/______/_
____/______/______/______/__"=._o--._   ;o|o;     _._;o;____/______/______/____
/______/______/______/______/____"=._o._; | ;_.--"o.--"_/______/______/______/_
____/______/______/______/______/_____"=.o|o_.--""___/______/______/______/____
/______/______/______/______/______/______/______/______/______/______/_____ /
''')

logo.pack()
logo.config(font=('length', 15))

line1 = tkinter.Label(window, text="Welcome to Treasure Island,n"
                                   "Your goal is to find the buried Treasure!")
line1.pack()
line1.config(font=('length', 15))

window.mainloop()
Asked By: moron

||

Answers:

for delay, you can use after method that take two arg
first arg time by millisecond and second arg is a function
more detail see

def callback():
  line1 = tkinter.Label(window, text="Welcome to Treasure Island,n"
                                   "Your goal is to find the buried Treasure!")
  line1.pack()
  line1.config(font=('length', 15))


window.after(3000, callback) #time by milisecond
 
Answered By: h sha

The code from h sha:

def callback():
  line1 = tkinter.Label(window, text="Welcome to Treasure Island,n"
                                   "Your goal is to find the buried Treasure!")
  line1.pack()
  line1.config(font=('length', 15))


tk.after(3000, callback)

The code that worked for me:

def callback():
  line1 = tkinter.Label(window, text="Welcome to Treasure Island,n"
                                   "Your goal is to find the buried Treasure!")
  line1.pack()
  line1.config(font=('length', 15))


window.after(3000, callback)
Answered By: moron
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.