简体   繁体   English

在 python 一段时间后关闭 tkmessagebox

[英]closing tkmessagebox after some time in python

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.我正在开发一个自动考勤系统,当学生扫描他的 RFID 标签时,他的出勤记录同时使用 tkmessagebox 弹出窗口显示欢迎消息。 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.用户将无法控制鼠标或键盘,我想将消息显示 2 秒并删除消息框。 Is there a way I can close the tkmessagebox pop-up like proposed?有没有办法像建议的那样关闭 tkmessagebox 弹出窗口?

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).我不认为它可以用tkMessageBox完成,因为这会创建一个模态对话框,并且您无权访问小部件 ID(因此可以通过编程方式销毁它)。

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.您需要将事件处理程序连接到 RFID 检测。 This is simulated by a button in the above code, and the event handler is the welcome() function.这是由上面代码中的一个按钮模拟的,事件处理程序是welcome()函数。 In welcome() a top level widget with a message is created.welcome()中创建了一个带有消息的顶级小部件。 The top level widget is destroyed after 2000 milliseconds (2 seconds) using .after() which registers a callback function to be called after a delay.顶级小部件在 2000 毫秒(2 秒)后使用.after()销毁,它注册了一个在延迟后调用的回调函数。

I have tried many solutions I have found online, none worked as I would have expected.我已经尝试了许多我在网上找到的解决方案,但没有一个像我预期的那样奏效。 Finally, I have found an easy solution:最后,我找到了一个简单的解决方案:

from tkinter import Tk
from tkinter.messagebox import Message 
from _tkinter import TclError

TIME_TO_WAIT = 2000 # in milliseconds 
root = Tk() 
root.withdraw()
try:
    root.after(TIME_TO_WAIT, root.destroy) 
    Message(title="your title", message="your message", master=root).show()
except TclError:
    pass

I know it's not optimal because I ignored the TclError, but it's the only thing that worked for me.我知道它不是最优的,因为我忽略了 TclError,但它是唯一对我有用的东西。 BTW I'm working with python 3.7顺便说一句,我正在使用 python 3.7

The small function below will do the job.下面的小 function 将完成这项工作。 By setting the type you can choose for: info, warning or error message box, the default is 'Info'.通过设置您可以选择的类型:信息、警告或错误消息框,默认为“信息”。 You can set also the timeout, the default is 2.5 seconds.您还可以设置超时,默认值为 2.5 秒。

def showMessage(message, type='info', timeout=2500):
    import tkinter as tk
    from tkinter import messagebox as msgb

    root = tk.Tk()
    root.withdraw()
    try:
        root.after(timeout, root.destroy)
        if type == 'info':
            msgb.showinfo('Info', message, master=root)
        elif type == 'warning':
            msgb.showwarning('Warning', message, master=root)
        elif type == 'error':
            msgb.showerror('Error', message, master=root)
    except:
        pass

Call the function as follow: For message type 'Info' and timeout of 2.5 seconds:按如下方式拨打 function:消息类型为“信息”且超时为 2.5 秒:

showMessage('Your message')

Or by your own settings for type message 'Error' and timeout 4 seconds:或者通过您自己的设置来输入消息“错误”和超时 4 秒:

showMessage('Your message', type='error', timeout=4000)

With Python3, you have to call the Toplevel() with the import name, Like :使用 Python3,您必须使用导入名称调用 Toplevel(),例如:

import tkinter

top = tkinter.Toplevel()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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