Home > Enterprise >  How do I add a time delay to a tkinter Label?
How do I add a time delay to a tkinter Label?

Time:08-18

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()

CodePudding user response:

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
 

CodePudding user response:

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))


tkinter.Tk().after(3000, callback)
  • Related