0
import tkinter as tk
import time
root=tk.Tk()
label1=tk.Label(text='hello',font='Calibri 25')
label1.pack()
time.sleep(3)
label1.configure(bg='green')
root.mainloop()

Again and again I say only I want to place label1 and after 3 seconds I want to change the background color but it doesnt work. Can you explain me the main problem and what can I do?

drum
  • 5,416
  • 7
  • 57
  • 91
  • Does this answer your question? [tkinter and time.sleep](https://stackoverflow.com/questions/10393886/tkinter-and-time-sleep) – gre_gor Dec 26 '22 at 22:34

1 Answers1

2

sleep should not be used in the main tkinter loop, as this causes the interface to freeze. It's better to use after.

import tkinter as tk

COLOR = 'green'

def lbl_clear():
    label1.configure(text="", bg=root['bg'])
    

def lbl_bg(color):
    label1.configure(bg=color)
    root.after(3000, lbl_clear)


root = tk.Tk()
label1 = tk.Label(text='hello', font='Calibri 25')
label1.pack()
root.after(3000, lbl_bg, COLOR)
root.mainloop()
Сергей Кох
  • 1,417
  • 12
  • 6
  • 13