简体   繁体   中英

How to add a time delay to a button in Tkinter?

first of all I would like to point out that I am new to tkinter and coding in general, so sorry if I make big mistakes. I am trying to make an image appear for a brief amount of time after I have clicked on a button, so I want to add a delay between the creation of the image and its deletion. This is the code:

fenetre=Tk()

Cadre1=Frame(fenetre, borderwidth=2,)
Cadre1.pack(side=RIGHT)
Cadre2=Frame(fenetre, borderwidth=2,)
Cadre2.pack(side=LEFT)

def rond():
    étoile=canvas.create_polygon(480,270,500,260,510,240,520,260,540,270,520,280,510,300,500,280)
    canvas.after(1000, canvas.delete(étoile))
    

canvas = Canvas(Cadre1, width=1500, height=1080, background='yellow')
canvas.pack()

bouton=Button(Cadre2,text='Attaque',command=rond ,relief=RAISED)
bouton.pack()

fenetre.mainloop()

The main problem I am encountering is in the "rond()" function. I have tried time.sleep and master.after but they all seem to simply extend the time where I can't click on the button instead of delaying the two parts of the function. I think that I do not quite understand how these functions work but I don't know what to change.

Thanks in advance for your answers

This will do the trick. the after function expects a function as parameter that is why I am using lambda function

def rond():
    print('Command accessed')
    etoile=canvas.create_polygon(480,270,500,260,510,240,520,260,540,270,520,280,510,300,500,280)
    canvas.after(2000, lambda: canvas.delete(etoile))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM