在Tkinter上显示标签固定的时间 [英] Displaying Label on Tkinter for a fixed amount of time

查看:139
本文介绍了在Tkinter上显示标签固定的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Tkinter在Python 2.7中创建一个GUI应用程序. 我有这段代码:

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)

我想知道是否可以在有限的时间内(〜3秒)内显示已成功添加购买"标签,然后标签消失.这是因为我有兴趣在当前的购买"之后添加一个新的购买",并且不希望成功消息重叠.

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.

推荐答案

有许多方法取决于项目模式,所有方法都基于语法:

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)

总破坏

如果您不想知道标签是否已创建,隐藏或为空,并且主要避免可能的内存泄漏(感谢Bryan Oakley的评论):

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() )

但是随后您需要为每次购买创建一个全新的Label.

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

隐藏并寻求

以下方法允许在不破坏标签的情况下禁用标签的显示.

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

然后您可以使用vis.place(x=150,y=460)

文本橡皮擦

另一种方法,也许不太有趣,除非您希望在容器小部件中保留一个空的标签:

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

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

(请注意,您可以在下次购买时将文本替换为vis.config(text='blabla'))

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

这篇关于在Tkinter上显示标签固定的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆