August 2018
Intermediate to advanced
366 pages
10h 14m
English
Alerts in tkinter are managed by the messagebox object and we can create one simply by asking messagebox to show one for us:
from tkinter import messagebox
def alert(title, message, kind='info', hidemain=True):
if kind not in ('error', 'warning', 'info'):
raise ValueError('Unsupported alert kind.')
show_method = getattr(messagebox, 'show{}'.format(kind))
show_method(title, message)
Once we have our alert helper in place, we can initialize a Tk interpreter and show as many alerts as we want:
from tkinter import Tk
Tk().withdraw()
alert('Hello', 'Hello World')
alert('Hello Again', 'Hello World 2', kind='warning')
If everything worked as expected, we should see a pop-up dialog and, once dismissed, a new one should come up with ...