Home > Enterprise >  How to remove a tkinter label after a specified amount of time?
How to remove a tkinter label after a specified amount of time?

Time:01-05

In my tkinter page i want a button that makes a label appear and after 3-5 seconds it dissapears.

from tkinter import *
import tkinter as tk
from tkinter.ttk import Button
import pyautogui

root= tk.Tk()

canvas1 = tk.Canvas(root, width=400, height=300, relief='raised')
canvas1.pack()

def get_square_root():
    
    label3 = tk.Label(root, text='Complete', font=('helvetica', 10))
    canvas1.create_window(200, 275, window=label3)
    

def on_click():
   label3.after(1000, label3.destroy())


button1 = tk.Button(text='Submit', command=lambda: [get_square_root(), on_click()], bg='brown', fg='white', font=('helvetica', 9, 'bold'))
canvas1.create_window(200, 250, window=button1)



root.mainloop()

Tried adding 2 functions to fit in 1 command however when running it python refuses to show the label at all.

CodePudding user response:

label3.after(1000, label4.destroy)

Thank you Bryan Oakley

Edit:

I finally made it so the labels being undefined by just adding the items in each function to 1 function

def one_command():

label4 = tk.Label(root, text='Complete', font=('helvetica', 10))
canvas1.create_window(200, 275, window=label4)

label4.after(1000, label4.destroy)

button1 = tk.Button(text='Submit', command=one_command, bg='brown', fg='white', font=('helvetica', 9, 'bold')) canvas1.create_window(200, 250, window=button1)

CodePudding user response:

You can see Label3 on fly. Actually, you are missing labe3.pack()

Tried adding 2 functions to fit in 1 command however when running it python refuses to show the label at all

Code:

from tkinter import *
import tkinter as tk
from tkinter.ttk import Button
 

root= tk.Tk()

canvas1 = tk.Canvas(root, width=400, height=300, relief='raised')
canvas1.pack()

label3 = tk.Label(root, text='Complete', font=('helvetica', 10))
label3.pack()

def get_square_root():
    canvas1.create_window(200, 275, window=label3)
    

def on_click():
   label3.after(1000, label3.destroy())
    
button1 = tk.Button(text='Submit', command=lambda: [get_square_root(), on_click()], bg='brown', fg='white', font=('helvetica', 9, 'bold'))
canvas1.create_window(200, 250, window=button1)

root.mainloop()

Output before you see Label:

enter image description here

Output after Label is disappeared:

enter image description here

  • Related