python tkinter messagebox 自动关闭,在Python中一段时间​​后关闭tkmessagebox

I am developing an automated attendance system where when a student scans his RFID tag, his attendance is recorded while showing a welcoming message using a tkmessagebox pop-up. The user will not have control of a mouse or keyboard and I would like to keep the message showing for 2 seconds and delete the message box. Is there a way I can close the tkmessagebox pop-up like proposed?

解决方案

I don't think that it can be done with tkMessageBox because this creates a modal dialog, and you do not have access to the widget id (so that it can be programmatically destroyed).

But it's not hard to create your own top level window, add some welcome message to it, then close it after a time period. Something like this:

from Tkinter import *

WELCOME_MSG = '''Welcome to this event.

Your attendance has been registered.

Don't forget your free lunch.'''

WELCOME_DURATION = 2000

def welcome():

top = Toplevel()

top.title('Welcome')

Message(top, text=WELCOME_MSG, padx=20, pady=20).pack()

top.after(WELCOME_DURATION, top.destroy)

root = Tk()

Button(root, text="Click to register", command=welcome).pack()

root.mainloop()

You need to hook up an event handler to the RFID detection. This is simulated by a button in the above code, and the event handler is the welcome() function. In welcome() a top level widget with a message is created. The top level widget is destroyed after 2000 milliseconds (2 seconds) using .after() which registers a callback function to be called after a delay.

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值