Skip to content

Commit 82b7570

Browse files
miss-islingtonserhiy-storchakaclaudehugovk
authored
[3.15] gh-86726: Document the full public API of tkinter (GH-151579) (GH-151649)
Replace the previously sparse reference documentation with full coverage of the public API of the tkinter package, written from the Tcl/Tk manual pages, the existing documentation and the module docstrings. * Doc/library/tkinter.rst gains a "Reference" section documenting every public class, method, function and constant of the core module -- the widgets, the Misc, Wm, Pack, Place, Grid, XView and YView mix-ins, the Variable and image classes, the module-level functions and the symbolic constants. * Doc/library/tkinter.ttk.rst, dialog.rst, tkinter.font.rst and the other module pages document their remaining classes, methods and functions. The descriptions are Python-oriented (correct return types -- tuples rather than Tcl lists, booleans, integers, None on cancellation, and so on) and were checked against the Tcl/Tk 9.1 manual pages and the implementation. versionadded, versionchanged and deprecated directives are added for the public API, determined from the git history relative to Python 3.0: the tkinter.ttk module (3.1); the Text, Wm, Menu and Misc methods exposing Tk 8.5 features (3.3); and the many later additions and behavior changes up to 3.15. The Tk version required by features added after Tk 8.6 is noted as well. The bundled Tcl/Tk version is updated to 9.0 and the manual-page links point at the tcl9.0 reference. -------- (cherry picked from commit 8b270b7) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent c0582db commit 82b7570

10 files changed

Lines changed: 5983 additions & 236 deletions

Doc/library/dialog.rst

Lines changed: 102 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Tkinter Dialogs
1+
Tkinter dialogs
22
===============
33

44
:mod:`!tkinter.simpledialog` --- Standard Tkinter input dialogs
@@ -36,6 +36,42 @@ functions for creating simple modal dialogs to get a value from the user.
3636
Default behaviour adds OK and Cancel buttons. Override for custom button
3737
layouts.
3838

39+
.. method:: validate()
40+
41+
Validate the data entered by the user.
42+
Return true if it is valid, in which case the dialog proceeds to
43+
:meth:`apply`; return false to keep the dialog open.
44+
The default implementation always returns true; override it to check the
45+
input.
46+
47+
.. method:: apply()
48+
49+
Process the data entered by the user.
50+
Called after :meth:`validate` succeeds and just before the dialog is
51+
destroyed.
52+
The default implementation does nothing; override it to act on or store
53+
the result.
54+
55+
.. method:: destroy()
56+
57+
Destroy the dialog window, clearing the reference to the widget that had
58+
the initial focus.
59+
60+
61+
.. class:: SimpleDialog(master, text='', buttons=[], default=None, cancel=None, title=None, class_=None)
62+
63+
A simple modal dialog that displays the message *text* above a row of push
64+
buttons whose labels are given by *buttons*, and returns the index of the
65+
button the user presses.
66+
*default* is the index of the button activated by the Return key, *cancel*
67+
the index returned when the window is closed through the window manager,
68+
*title* the window title, and *class_* the Tk class name of the window.
69+
70+
.. method:: go()
71+
72+
Display the dialog, wait until the user presses a button or closes the
73+
window, and return the index of the chosen button.
74+
3975

4076

4177
:mod:`!tkinter.filedialog` --- File selection dialogs
@@ -51,7 +87,7 @@ functions for creating simple modal dialogs to get a value from the user.
5187
The :mod:`!tkinter.filedialog` module provides classes and factory functions for
5288
creating file/directory selection windows.
5389

54-
Native Load/Save Dialogs
90+
Native load/save dialogs
5591
------------------------
5692

5793
The following classes and functions provide file dialog windows that combine a
@@ -77,34 +113,46 @@ listed below:
77113
**Static factory functions**
78114

79115
The below functions when called create a modal, native look-and-feel dialog,
80-
wait for the user's selection, then return the selected value(s) or ``None`` to the
81-
caller.
116+
wait for the user's selection, and return it.
117+
The exact return value depends on the function (see below); when the dialog is
118+
cancelled it is an empty string, an empty tuple, an empty list or ``None``.
82119

83120
.. function:: askopenfile(mode="r", **options)
84121
askopenfiles(mode="r", **options)
85122
86-
The above two functions create an :class:`Open` dialog and return the opened
87-
file object(s) in read-only mode.
123+
Create an :class:`Open` dialog.
124+
:func:`askopenfile` returns the opened file object, or ``None`` if the
125+
dialog is cancelled.
126+
:func:`askopenfiles` returns a list of the opened file objects, or an empty
127+
list if cancelled.
128+
The files are opened in mode *mode* (read-only ``'r'`` by default).
88129

89130
.. function:: asksaveasfile(mode="w", **options)
90131

91-
Create a :class:`SaveAs` dialog and return a file object opened in write-only mode.
132+
Create a :class:`SaveAs` dialog and return the opened file object, or
133+
``None`` if the dialog is cancelled.
134+
The file is opened in mode *mode* (``'w'`` by default).
92135

93136
.. function:: askopenfilename(**options)
94137
askopenfilenames(**options)
95138
96-
The above two functions create an :class:`Open` dialog and return the
97-
selected filename(s) that correspond to existing file(s).
139+
Create an :class:`Open` dialog.
140+
:func:`askopenfilename` returns the selected filename as a string, or an
141+
empty string if the dialog is cancelled.
142+
:func:`askopenfilenames` returns a tuple of the selected filenames, or an
143+
empty tuple if cancelled.
98144

99145
.. function:: asksaveasfilename(**options)
100146

101-
Create a :class:`SaveAs` dialog and return the selected filename.
147+
Create a :class:`SaveAs` dialog and return the selected filename as a
148+
string, or an empty string if the dialog is cancelled.
102149

103150
.. function:: askdirectory(**options)
104151

105-
| Prompt user to select a directory.
106-
| Additional keyword option:
107-
| *mustexist* - determines if selection must be an existing directory.
152+
Prompt the user to select a directory, and return its path as a string, or
153+
an empty string if the dialog is cancelled.
154+
Additional keyword option: *mustexist* - if true, the user may only select
155+
an existing directory (false by default).
108156

109157
.. class:: Open(master=None, **options)
110158
SaveAs(master=None, **options)
@@ -168,6 +216,13 @@ These do not emulate the native look-and-feel of the platform.
168216

169217
Exit dialog returning current selection.
170218

219+
.. method:: ok_command()
220+
221+
Called when the user confirms the current selection.
222+
The base implementation accepts the selection and closes the dialog;
223+
:class:`LoadFileDialog` and :class:`SaveFileDialog` override it to check
224+
the selection first.
225+
171226
.. method:: quit(how=None)
172227

173228
Exit dialog returning filename, if any.
@@ -222,6 +277,40 @@ is the base class for dialogs defined in other supporting modules.
222277
Render the Dialog window.
223278

224279

280+
:mod:`!tkinter.dialog` --- Classic Tk dialog boxes
281+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
282+
283+
.. module:: tkinter.dialog
284+
:synopsis: A simple dialog box built on the classic Tk widgets.
285+
286+
**Source code:** :source:`Lib/tkinter/dialog.py`
287+
288+
--------------
289+
290+
The :mod:`!tkinter.dialog` module provides a simple modal dialog box built on
291+
the classic (non-themed) Tk widgets.
292+
293+
.. data:: DIALOG_ICON
294+
295+
The name of the default bitmap (``'questhead'``) displayed by a
296+
:class:`Dialog`.
297+
298+
.. class:: Dialog(master=None, cnf={}, **kw)
299+
300+
Display a modal dialog box built from the classic (non-themed) Tk widgets
301+
and wait for the user to press one of its buttons.
302+
The options, given through *cnf* or as keyword arguments, include *title*
303+
(the window title), *text* (the message), *bitmap* (an icon,
304+
:data:`DIALOG_ICON` by default), *default* (the index of the default button)
305+
and *strings* (the sequence of button labels).
306+
After construction, the :attr:`!num` attribute holds the index of the button
307+
the user pressed.
308+
309+
.. method:: destroy()
310+
311+
Destroy the dialog window.
312+
313+
225314
.. seealso::
226315

227316
Modules :mod:`tkinter.messagebox`, :ref:`tut-files`

Doc/library/tkinter.colorchooser.rst

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,26 @@ the :class:`~tkinter.commondialog.Dialog` class.
1515

1616
.. class:: Chooser(master=None, **options)
1717

18+
The class implementing the modal color-choosing dialog.
19+
Most applications use the :func:`askcolor` convenience function rather than
20+
instantiating this class directly.
21+
1822
.. function:: askcolor(color=None, **options)
1923

20-
Create a color choosing dialog. A call to this method will show the window,
21-
wait for the user to make a selection, and return the selected color (or
22-
``None``) to the caller.
24+
Show a modal color-choosing dialog and return the chosen color.
25+
*color* is the color selected when the dialog opens.
26+
The return value is a tuple ``((r, g, b), hexstr)``, where ``r``, ``g`` and
27+
``b`` are the red, green and blue components as integers in the range 0–255
28+
and *hexstr* is the equivalent Tk color string, such as ``'#ff8000'``.
29+
If the user cancels the dialog, ``(None, None)`` is returned.
30+
31+
.. versionchanged:: 3.10
32+
The RGB values in the returned color are now integers in the range 0–255
33+
instead of floats.
2334

2435

2536
.. seealso::
2637

2738
Module :mod:`tkinter.commondialog`
2839
Tkinter standard dialog module
40+

Doc/library/tkinter.dnd.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,18 @@ Selection of a target object occurs as follows:
4848

4949
.. method:: on_motion(event)
5050

51-
Inspect area below mouse for target objects while drag is performed.
51+
Inspect area below mouse for target objects while a drag
52+
is performed.
5253

5354
.. method:: on_release(event)
5455

5556
Signal end of drag when the release pattern is triggered.
5657

5758
.. function:: dnd_start(source, event)
5859

59-
Factory function for drag-and-drop process.
60+
Factory function for the drag-and-drop process.
61+
Return the :class:`DndHandler` instance managing the drag, or ``None`` if a
62+
drag could not be started.
6063

6164
.. seealso::
6265

Doc/library/tkinter.font.rst

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ The different font weights and slants are:
2626
fonts as a single object, rather than specifying a font by its attributes
2727
with each occurrence.
2828

29+
.. versionchanged:: 3.10
30+
Two fonts now compare equal (``==``) only when both are :class:`Font`
31+
instances with the same name belonging to the same Tcl interpreter.
32+
2933
arguments:
3034

3135
| *font* - font specifier tuple (family, size, options)
@@ -34,7 +38,7 @@ The different font weights and slants are:
3438
3539
additional keyword options (ignored if *font* is specified):
3640

37-
| *family* - font family i.e. Courier, Times
41+
| *family* - font family, for example, Courier, Times
3842
| *size* - font size
3943
| If *size* is positive it is interpreted as size in points.
4044
| If *size* is a negative number its absolute value is treated
@@ -46,15 +50,24 @@ The different font weights and slants are:
4650
4751
.. method:: actual(option=None, displayof=None)
4852

49-
Return the attributes of the font.
53+
Return the actual attributes of the font, which may differ from the
54+
requested ones because of platform limitations.
55+
With no *option*, return a dictionary of all the attributes; if *option*
56+
is given, return the value of that single attribute.
5057

5158
.. method:: cget(option)
5259

5360
Retrieve an attribute of the font.
5461

5562
.. method:: config(**options)
63+
:no-typesetting:
64+
65+
.. method:: configure(**options)
66+
67+
Modify one or more attributes of the font.
68+
With no arguments, return a dictionary of the current attributes.
5669

57-
Modify attributes of the font.
70+
:meth:`config` is an alias of :meth:`!configure`.
5871

5972
.. method:: copy()
6073

@@ -63,12 +76,15 @@ The different font weights and slants are:
6376
.. method:: measure(text, displayof=None)
6477

6578
Return amount of space the text would occupy on the specified display
66-
when formatted in the current font. If no display is specified then the
67-
main application window is assumed.
79+
when formatted in the current font, as an integer number of pixels.
80+
If no display is specified then the main application window is assumed.
6881

6982
.. method:: metrics(*options, **kw)
7083

7184
Return font-specific data.
85+
With no options, return a dictionary mapping each metric name to its
86+
integer value; if one option name is given, return that metric's value as
87+
an integer.
7288
Options include:
7389

7490
*ascent* - distance between baseline and highest point that a
@@ -84,15 +100,17 @@ The different font weights and slants are:
84100

85101
.. function:: families(root=None, displayof=None)
86102

87-
Return the different font families.
103+
Return a tuple of the names of the available font families.
88104

89105
.. function:: names(root=None)
90106

91-
Return the names of defined fonts.
107+
Return a tuple of the names of all the defined fonts.
92108

93109
.. function:: nametofont(name, root=None)
94110

95-
Return a :class:`Font` representation of a tk named font.
111+
Return a :class:`Font` representation of the existing named font *name*.
112+
*root* is the widget whose Tcl interpreter owns the font; if omitted, the
113+
default root window is used.
96114

97115
.. versionchanged:: 3.10
98116
The *root* parameter was added.

Doc/library/tkinter.messagebox.rst

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
--------------
1010

1111
The :mod:`!tkinter.messagebox` module provides a template base class as well as
12-
a variety of convenience methods for commonly used configurations. The message
13-
boxes are modal and will return a subset of (``True``, ``False``, ``None``,
14-
:data:`OK`, :data:`CANCEL`, :data:`YES`, :data:`NO`) based on
15-
the user's selection. Common message box styles and layouts include but are not
16-
limited to:
12+
a variety of convenience methods for commonly used configurations.
13+
The message boxes are modal: each blocks until the user responds, then returns
14+
a value that depends on the function.
15+
The ``show*`` functions and :meth:`Message.show` return the symbolic name of
16+
the button the user pressed, as a string (such as :data:`OK` or :data:`YES`),
17+
while the ``ask*`` functions return a :class:`bool` or ``None`` (see each
18+
function below).
19+
Common message box styles and layouts include but are not limited to:
1720

1821
.. figure:: tk_msg.png
1922

@@ -66,6 +69,10 @@ limited to:
6669
Arranges for a :ref:`predefined set of buttons <messagebox-types>`
6770
to be displayed.
6871

72+
.. note::
73+
74+
Tk 8.6 added the *command* option.
75+
6976
.. method:: show(**options)
7077

7178
Display a message window and wait for the user to select one of the buttons. Then return the symbolic name of the selected button.

0 commit comments

Comments
 (0)