Closing tkmessagebox after a while in python
I am developing an automated attendance system, when a student views their RFID tag, their attendance is recorded showing a welcome message using the tkmessagebox popup. The user will have no control over the mouse or keyboard and I would like to leave a message for 2 seconds and delete the message box. Is there a way to close the tkmessagebox popup as suggested?
source to share
I don't think it can be done with tkMessageBox
, because it creates a modal dialog and you don't have access to the widget id (so that it can be destroyed programmatically).
But it's not hard to create your own top-level window, add a welcome message to it, and then close it after a certain period of time. Something like that:
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 connect your event handler to RFID detection. This is simulated by the button in the above code, and the event handler is a function welcome()
. The welcome()
created top-level widget with the message. The top-level widget is destroyed after 2000 milliseconds (2 seconds) using .after()
that registers a callback function that gets called after the delay.
source to share