Skip to content

Commit ad38cf8

Browse files
gh-59396: Modernize tkinter.simpledialog (GH-151848)
Rework SimpleDialog and Dialog to match the look and feel of the native Tk dialogs ::tk_dialog and ::tk::MessageBox. * SimpleDialog is a Python port of ::tk_dialog and Dialog a base class modelled on ::tk::MessageBox. Both adopt the message-box keyboard conventions: button accelerators, a default ring that follows the keyboard focus, and a <Return> binding that invokes the focused button. * Both classes gain a use_ttk parameter that selects the classic Tk or the themed ttk widgets. It controls the widget set and the appearance that the two procedures style differently, but not the keyboard behaviour. * Update _place_window with the Tk 9.1 placement refinements. * The new helpers _temp_grab_focus (a modal grab/focus context manager), _underline_ampersand and _find_alt_key_target (ports of the Tk accelerator-key procedures) can be reused by other tkinter dialogs, as _setup_dialog already is. * Fix several defects uncovered while comparing with the Tcl sources. * Improve the simpledialog demo. Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 868d9a8 commit ad38cf8

5 files changed

Lines changed: 856 additions & 140 deletions

File tree

Doc/library/dialog.rst

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ The :mod:`!tkinter.simpledialog` module contains convenience classes and
1515
functions for creating simple modal dialogs to get a value from the user.
1616

1717

18-
.. function:: askfloat(title, prompt, *, initialvalue=None, minvalue=None, maxvalue=None, parent=None)
19-
askinteger(title, prompt, *, initialvalue=None, minvalue=None, maxvalue=None, parent=None)
20-
askstring(title, prompt, *, initialvalue=None, show=None, parent=None)
18+
.. function:: askfloat(title, prompt, *, initialvalue=None, minvalue=None, maxvalue=None, parent=None, use_ttk=True)
19+
askinteger(title, prompt, *, initialvalue=None, minvalue=None, maxvalue=None, parent=None, use_ttk=True)
20+
askstring(title, prompt, *, initialvalue=None, show=None, parent=None, use_ttk=True)
2121
2222
Prompt the user to enter a value of the desired type and return it, or
2323
``None`` if the dialog is cancelled.
@@ -29,12 +29,22 @@ functions for creating simple modal dialogs to get a value from the user.
2929
*maxvalue*, which bound the accepted value.
3030
:func:`askstring` also accepts *show*, a character used to mask the entered
3131
text, for example ``'*'`` to hide a password.
32+
They use the themed :mod:`tkinter.ttk` widgets; pass ``use_ttk=False`` for
33+
the classic widgets.
3234

33-
.. class:: Dialog(parent, title=None)
35+
.. class:: Dialog(parent, title=None, *, use_ttk=False)
3436

3537
The base class for custom dialogs.
3638
Instantiating it shows the dialog modally and returns once the user closes
3739
it; the entered value is then available in the :attr:`!result` attribute.
40+
When *use_ttk* is false (the default), the dialog is built from the classic
41+
:mod:`tkinter` widgets, modelled on the classic ``tk_dialog``; when true,
42+
from the themed :mod:`tkinter.ttk` widgets, modelled on the Tk message box.
43+
The default is classic for compatibility, since the themed widgets set a
44+
themed background that classic widgets added in :meth:`body` would not match.
45+
46+
.. versionchanged:: next
47+
Added the *use_ttk* parameter.
3848

3949
.. attribute:: result
4050

@@ -74,14 +84,32 @@ functions for creating simple modal dialogs to get a value from the user.
7484
the initial focus.
7585

7686

77-
.. class:: SimpleDialog(master, text='', buttons=[], default=None, cancel=None, title=None, class_=None)
87+
.. class:: SimpleDialog(master, text='', buttons=[], default=None, cancel=None, title=None, class_=None, *, bitmap=None, detail='', use_ttk=True)
7888

7989
A simple modal dialog that displays the message *text* above a row of push
80-
buttons whose labels are given by *buttons*, and returns the index of the
81-
button the user presses.
82-
*default* is the index of the button activated by the Return key, *cancel*
83-
the index returned when the window is closed through the window manager,
84-
*title* the window title, and *class_* the Tk class name of the window.
90+
buttons given by *buttons*, and returns the index of the button the user
91+
presses.
92+
Each entry of *buttons* is either a button label, or a mapping of button
93+
options such as ``{'text': 'OK', 'underline': 0}``; an ``underline`` option
94+
makes :kbd:`Alt` plus the underlined character invoke the button.
95+
*default* is the index of the default button, activated by the Return key
96+
when no button has the focus, *cancel* the index returned when the window is
97+
closed through the window manager, *title* the window title, and *class_*
98+
the Tk class name of the window.
99+
*bitmap* is the name of a bitmap displayed beside the message
100+
(for example ``'warning'`` or ``'question'``); the standard names
101+
``'error'``, ``'info'``, ``'question'`` and ``'warning'`` are shown as
102+
themed icons when *use_ttk* is true.
103+
*detail* is a secondary message displayed below *text*.
104+
When *use_ttk* is true (the default), the dialog is built from the themed
105+
:mod:`tkinter.ttk` widgets, modelled on the Tk message box; when false, from
106+
the classic :mod:`tkinter` widgets, modelled on ``tk_dialog``.
107+
108+
.. versionchanged:: next
109+
The dialog is now built from the themed :mod:`tkinter.ttk` widgets by
110+
default, instead of the classic :mod:`tkinter` widgets.
111+
Added the *bitmap*, *detail* and *use_ttk* parameters.
112+
Entries of *buttons* may be mappings of button options.
85113

86114
.. method:: go()
87115

Doc/whatsnew/3.16.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,22 @@ tkinter
220220
options as keyword arguments, which can also override its default appearance.
221221
(Contributed by Serhiy Storchaka in :gh:`101284`.)
222222

223+
* The :mod:`tkinter.simpledialog` dialogs were modernized to match the look
224+
and feel of the native Tk dialogs.
225+
:class:`!tkinter.simpledialog.SimpleDialog` and the
226+
:func:`~tkinter.simpledialog.askinteger`,
227+
:func:`~tkinter.simpledialog.askfloat` and
228+
:func:`~tkinter.simpledialog.askstring` dialogs are now built from the themed
229+
:mod:`tkinter.ttk` widgets instead of the classic :mod:`tkinter` widgets;
230+
the :class:`!tkinter.simpledialog.Dialog` base class still defaults to the
231+
classic widgets for compatibility. Both :class:`!Dialog` and
232+
:class:`!SimpleDialog` gained a *use_ttk* parameter that selects between the
233+
classic Tk widgets and the themed ttk widgets. :class:`!SimpleDialog` also
234+
gained *bitmap* and
235+
*detail* parameters, draws the standard icons with themed images in the
236+
ttk version, and accepts mappings of button options as *buttons* entries.
237+
(Contributed by Serhiy Storchaka in :gh:`59396`.)
238+
223239
xml
224240
---
225241

0 commit comments

Comments
 (0)