在Python中一段时间​​后关闭tkmessagebox [英] closing tkmessagebox after some time in python

查看:409
本文介绍了在Python中一段时间​​后关闭tkmessagebox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一种自动出勤系统,当学生扫描他的RFID标签时,会使用tkmessagebox弹出窗口显示欢迎消息的同时记录其出勤情况.用户将无法控制鼠标或键盘,我想让消息显示2秒钟,然后删除消息框.有没有办法像建议的那样关闭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?

推荐答案

我不认为可以使用tkMessageBox完成此操作,因为这会创建一个模式对话框,并且您无权访问小部件ID(因此可以通过程序将其销毁).

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

您需要将事件处理程序连接到RFID检测.这是由上面代码中的按钮模拟的,事件处理程序是welcome()函数.在welcome()中,创建带有消息的顶级窗口小部件.使用 .after(),在2000毫秒(2秒)后破坏了顶层窗口小部件. 会注册一个延迟后要调用的回调函数.

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.

这篇关于在Python中一段时间​​后关闭tkmessagebox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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