August 2018
Intermediate to advanced
366 pages
10h 14m
English
We can create a dialog function to hide the minor differences between dialog types and call the appropriate dialog depending on the kind of request:
from tkinter import messagebox from tkinter import simpledialog from tkinter import filedialog def dialog(ask, title, message=None, **kwargs): for widget in (messagebox, simpledialog, filedialog): show = getattr(widget, 'ask{}'.format(ask), None) if show: break else: raise ValueError('Unsupported type of dialog: {}'.format(ask)) options = dict(kwargs, title=title) for arg, replacement in dialog._argsmap.get(widget, {}).items(): options[replacement] = locals()[arg] return show(**options) dialog._argsmap = { messagebox: {'message': 'message'}, simpledialog: {'message': 'prompt'} ...