closing tkmessagebox after some time in python closing tkmessagebox after some time in python tkinter tkinter

closing tkmessagebox after some time in python


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 = 2000def 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.


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 Tkfrom tkinter.messagebox import Message from _tkinter import TclErrorTIME_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.BTW I'm working with python 3.7


With Python3, you have to call the Toplevel() with the import name, Like :

import tkintertop = tkinter.Toplevel()