Md. Ameen Yoosuf
Md. Ameen Yoosuf

Reputation: 33

Displaying Label on Tkinter for a fixed amount of time

I'm creating a GUI application in Python 2.7 using Tkinter. I have this piece of code:

vis=Label(pur,text='Purchase Added successfully',font=(8))
vis.place(x=150,y=460)

I wanna know if there's any way to display the Label 'Purchase Added Successfully' for a limited amount of time(~3 seconds) and then it would disappear. This is because I'm interested in adding a new 'purchase' after the current one, and don't want the success messages to overlap.

Upvotes: 1

Views: 1964

Answers (1)

PRMoureu
PRMoureu

Reputation: 13327

There are many ways depending on the project pattern, all based on the syntax :

vis=Label(pur,text='Purchase Added successfully',font=(8))
vis.place(x=150,y=460)
vis.after(3000, function_to_execute)

Total Destruction

If you don't want to wonder whether the label is already created, hidden or empty, and mostly avoid possible memory leaks (thanks to Bryan Oakley comment):

vis.after(3000, lambda: vis.destroy() )

But then you need to create a fresh new Label for every purchase.


Hide and Seek

The following method allows to disable the display of the Label without destroying it.

vis.after(3000, lambda: vis.place_forget() )
#vis.after(3000, lambda: vis.grid_forget() ) # if grid() was used
#vis.after(3000, lambda: vis.pack_forget() ) # if pack() was used

Then you can enable it again for the next purchase, with vis.place(x=150,y=460)


Text Eraser

Another way, maybe less interesting, unless you prefer keeping an empty Label in the container widget:

vis.after(3000, lambda: vis.config(text='') )

(Note that you can replace the text with vis.config(text='blabla') for the next purchase)

Upvotes: 2

Related Questions