0

I am making a dice roller, and I wish to make the result of the dice roll disappear after 5 seconds, can anyone help?

from tkinter import *
import random
root=Tk()
root.geometry('700x500')
root.title("Dice roller")
bg = PhotoImage(file = "background.png")
Label1 = Label(root, image = bg)
Label1.place (x = 0, y=0)
l = Label(root, text="Welcome to the dice roller, select the dice you want to roll!",bg = "gold",fg = "goldenrod4", relief = "raised", font=("Arial Bold",12))
l.pack()


def buttonFunction1():
    r1 = random.randint(1, 6)
    L = Label(root, text= r1, bg = "red", relief = "solid", cursor = "target", font = ("Arial Bold", 10)) 
###That was the label I want to make disappear after 5 seconds###
    L.place(relx = 0.8, rely = 0.14, anchor = 'sw') 
b = Button(root, text="cube dice", command= buttonFunction1)
b.pack()
Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44

1 Answers1

0

dont know what is going on here, but tried different ways and this one seems to work:


from PIL import ImageTk, Image

import time

import tkinter as tk
import random


root=tk.Tk()
root.geometry('700x500')
root.title("Dice roller")
# bg = ImageTk.PhotoImage(file = "background.png")
bg = ImageTk.PhotoImage(file = r"fff.png")
Label1 = tk.Label(root, image = bg)



Label1.place (x = 0, y=0)
l = tk.Label(root, text="Welcome to the dice roller, select the dice you want to roll!",bg = "gold",fg = "goldenrod4", relief = "raised", font=("Arial Bold",12))
l.pack()


def destr(label):
    # time.sleep(20)  # this one FREEZES the entire app: cant press button while sleeping AND doesnt resume
    label.destroy()
    pass

def buttonFunction1():
    r1 = random.randint(1, 6)
    L = tk.Label(root, text= r1, bg = "red", relief = "solid", cursor = "target", font = ("Arial Bold", 10)) 
###That was the label I want to make disappear after 5 seconds###
    L.place(relx = 0.8, rely = 0.14, anchor = 'sw') 
    

    L.after(5000, destr, L)  ## after 5 seconds (more or less) invoke destr(L), you can keep pressing the button


    
b = tk.Button(root, text="cube dice", command= buttonFunction1)
b.pack()

root.mainloop()
pippo1980
  • 2,181
  • 3
  • 14
  • 30