Skip to content

Commit 80428ed

Browse files
patel-nikhilJulienPalard
authored andcommitted
bpo-25237: Documentation for tkinter modules (GH-1870)
1 parent 0711642 commit 80428ed

File tree

10 files changed

+479
-15
lines changed

10 files changed

+479
-15
lines changed

Doc/library/dialog.rst

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
Tkinter Dialogs
2+
===============
3+
4+
:mod:`tkinter.simpledialog` --- Standard Tkinter input dialogs
5+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
.. module:: tkinter.simpledialog
8+
:platform: Tk
9+
:synopsis: Simple dialog windows
10+
11+
**Source code:** :source:`Lib/tkinter/simpledialog.py`
12+
13+
--------------
14+
15+
The :mod:`tkinter.simpledialog` module contains convenience classes and
16+
functions for creating simple modal dialogs to get a value from the user.
17+
18+
19+
.. function:: askfloat(title, prompt, **kw)
20+
askinteger(title, prompt, **kw)
21+
askstring(title, prompt, **kw)
22+
23+
The above three functions provide dialogs that prompt the user to enter a value
24+
of the desired type.
25+
26+
.. class:: Dialog(parent, title=None)
27+
28+
The base class for custom dialogs.
29+
30+
.. method:: body(master)
31+
32+
Override to construct the dialog's interface and return the widget that
33+
should have initial focus.
34+
35+
.. method:: buttonbox()
36+
37+
Default behaviour adds OK and Cancel buttons. Override for custom button
38+
layouts.
39+
40+
41+
42+
:mod:`tkinter.filedialog` --- File selection dialogs
43+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
45+
.. module:: tkinter.filedialog
46+
:platform: Tk
47+
:synopsis: Dialog classes for file selection
48+
49+
**Source code:** :source:`Lib/tkinter/filedialog.py`
50+
51+
--------------
52+
53+
The :mod:`tkinter.filedialog` module provides classes and factory functions for
54+
creating file/directory selection windows.
55+
56+
Native Load/Save Dialogs
57+
------------------------
58+
59+
The following classes and functions provide file dialog windows that combine a
60+
native look-and-feel with configuration options to customize behaviour.
61+
The following keyword arguments are applicable to the classes and functions
62+
listed below:
63+
64+
| *parent* - the window to place the dialog on top of
65+
66+
| *title* - the title of the window
67+
68+
| *initialdir* - the directory that the dialog starts in
69+
70+
| *initialfile* - the file selected upon opening of the dialog
71+
72+
| *filetypes* - a sequence of (label, pattern) tuples, '*' wildcard is allowed
73+
74+
| *defaultextension* - default extension to append to file (save dialogs)
75+
76+
| *multiple* - when True, selection of multiple items is allowed
77+
78+
79+
**Static factory functions**
80+
81+
The below functions when called create a modal, native look-and-feel dialog,
82+
wait for the user's selection, then return the selected value(s) or ``None`` to the
83+
caller.
84+
85+
.. function:: askopenfile(mode="r", **options)
86+
askopenfiles(mode="r", **options)
87+
88+
The above two functions create an :class:`Open` dialog and return the opened
89+
file object(s) in read-only mode.
90+
91+
.. function:: asksaveasfile(mode="w", **options)
92+
93+
Create a :class:`SaveAs` dialog and return a file object opened in write-only mode.
94+
95+
.. function:: askopenfilename(**options)
96+
askopenfilenames(**options)
97+
98+
The above two functions create an :class:`Open` dialog and return the
99+
selected filename(s) that correspond to existing file(s).
100+
101+
.. function:: asksaveasfilename(**options)
102+
103+
Create a :class:`SaveAs` dialog and return the selected filename.
104+
105+
.. function:: askdirectory(**options)
106+
107+
| Prompt user to select a directory.
108+
| Additional keyword option:
109+
| *mustexist* - determines if selection must be an existing directory.
110+
111+
.. class:: Open(master=None, **options)
112+
SaveAs(master=None, **options)
113+
114+
The above two classes provide native dialog windows for saving and loading
115+
files.
116+
117+
**Convenience classes**
118+
119+
The below classes are used for creating file/directory windows from scratch.
120+
These do not emulate the native look-and-feel of the platform.
121+
122+
.. class:: Directory(master=None, **options)
123+
124+
Create a dialog prompting the user to select a directory.
125+
126+
.. note:: The *FileDialog* class should be subclassed for custom event
127+
handling and behaviour.
128+
129+
.. class:: FileDialog(master, title=None)
130+
131+
Create a basic file selection dialog.
132+
133+
.. method:: cancel_command(event=None)
134+
135+
Trigger the termination of the dialog window.
136+
137+
.. method:: dirs_double_event(event)
138+
139+
Event handler for double-click event on directory.
140+
141+
.. method:: dirs_select_event(event)
142+
143+
Event handler for click event on directory.
144+
145+
.. method:: files_double_event(event)
146+
147+
Event handler for double-click event on file.
148+
149+
.. method:: files_select_event(event)
150+
151+
Event handler for single-click event on file.
152+
153+
.. method:: filter_command(event=None)
154+
155+
Filter the files by directory.
156+
157+
.. method:: get_filter()
158+
159+
Retrieve the file filter currently in use.
160+
161+
.. method:: get_selection()
162+
163+
Retrieve the currently selected item.
164+
165+
.. method:: go(dir_or_file=os.curdir, pattern="*", default="", key=None)
166+
167+
Render dialog and start event loop.
168+
169+
.. method:: ok_event(event)
170+
171+
Exit dialog returning current selection.
172+
173+
.. method:: quit(how=None)
174+
175+
Exit dialog returning filename, if any.
176+
177+
.. method:: set_filter(dir, pat)
178+
179+
Set the file filter.
180+
181+
.. method:: set_selection(file)
182+
183+
Update the current file selection to *file*.
184+
185+
186+
.. class:: LoadFileDialog(master, title=None)
187+
188+
A subclass of FileDialog that creates a dialog window for selecting an
189+
existing file.
190+
191+
.. method:: ok_command()
192+
193+
Test that a file is provided and that the selection indicates an
194+
already existing file.
195+
196+
.. class:: SaveFileDialog(master, title=None)
197+
198+
A subclass of FileDialog that creates a dialog window for selecting a
199+
destination file.
200+
201+
.. method:: ok_command()
202+
203+
Test whether or not the selection points to a valid file that is not a
204+
directory. Confirmation is required if an already existing file is
205+
selected.
206+
207+
:mod:`tkinter.commondialog` --- Dialog window templates
208+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
209+
210+
.. module:: tkinter.commondialog
211+
:platform: Tk
212+
:synopsis: Tkinter base class for dialogs
213+
214+
**Source code:** :source:`Lib/tkinter/commondialog.py`
215+
216+
--------------
217+
218+
The :mod:`tkinter.commondialog` module provides the :class:`Dialog` class that
219+
is the base class for dialogs defined in other supporting modules.
220+
221+
.. class:: Dialog(master=None, **options)
222+
223+
.. method:: show(color=None, **options)
224+
225+
Render the Dialog window.
226+
227+
228+
.. seealso::
229+
230+
Modules :mod:`tkinter.messagebox`, :ref:`tut-files`

Doc/library/tk.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@ alternatives, see the :ref:`other-gui-packages` section.
3333
.. toctree::
3434

3535
tkinter.rst
36+
tkinter.colorchooser.rst
37+
tkinter.font.rst
38+
dialog.rst
39+
tkinter.messagebox.rst
40+
tkinter.scrolledtext.rst
41+
tkinter.dnd.rst
3642
tkinter.ttk.rst
3743
tkinter.tix.rst
38-
tkinter.scrolledtext.rst
3944
idle.rst
4045
othergui.rst
4146

4247
.. Other sections I have in mind are
4348
Tkinter internals
44-
Freezing Tkinter applications
45-
46-
49+
Freezing Tkinter applications

Doc/library/tk_msg.png

19.2 KB
Loading

Doc/library/tkinter.colorchooser.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
:mod:`tkinter.colorchooser` --- Color choosing dialog
2+
=====================================================
3+
4+
.. module:: tkinter.colorchooser
5+
:platform: Tk
6+
:synopsis: Color choosing dialog
7+
8+
**Source code:** :source:`Lib/tkinter/colorchooser.py`
9+
10+
--------------
11+
12+
The :mod:`tkinter.colorchooser` module provides the :class:`Chooser` class
13+
as an interface to the native color picker dialog. ``Chooser`` implements
14+
a modal color choosing dialog window. The ``Chooser`` class inherits from
15+
the :class:`~tkinter.commondialog.Dialog` class.
16+
17+
.. class:: Chooser(master=None, **options)
18+
19+
.. function:: askcolor(color=None, **options)
20+
21+
Create a color choosing dialog. A call to this method will show the window,
22+
wait for the user to make a selection, and return the selected color (or
23+
``None``) to the caller.
24+
25+
26+
.. seealso::
27+
28+
Module :mod:`tkinter.commondialog`
29+
Tkinter standard dialog module

Doc/library/tkinter.dnd.rst

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
:mod:`tkinter.dnd` --- Drag and drop support
2+
============================================
3+
4+
.. module:: tkinter.dnd
5+
:platform: Tk
6+
:synopsis: Tkinter drag-and-drop interface
7+
8+
**Source code:** :source:`Lib/tkinter/dnd.py`
9+
10+
--------------
11+
12+
.. note:: This is experimental and due to be deprecated when it is replaced
13+
with the Tk DND.
14+
15+
The :mod:`tkinter.dnd` module provides drag-and-drop support for objects within
16+
a single application, within the same window or between windows. To enable an
17+
object to be dragged, you must create an event binding for it that starts the
18+
drag-and-drop process. Typically, you bind a ButtonPress event to a callback
19+
function that you write (see :ref:`Bindings-and-Events`). The function should
20+
call :func:`dnd_start`, where 'source' is the object to be dragged, and 'event'
21+
is the event that invoked the call (the argument to your callback function).
22+
23+
Selection of a target object occurs as follows:
24+
25+
#. Top-down search of area under mouse for target widget
26+
27+
* Target widget should have a callable *dnd_accept* attribute
28+
* If *dnd_accept* is not present or returns None, search moves to parent widget
29+
* If no target widget is found, then the target object is None
30+
31+
2. Call to *<old_target>.dnd_leave(source, event)*
32+
#. Call to *<new_target>.dnd_enter(source, event)*
33+
#. Call to *<target>.dnd_commit(source, event)* to notify of drop
34+
#. Call to *<source>.dnd_end(target, event)* to signal end of drag-and-drop
35+
36+
37+
.. class:: DndHandler(source, event)
38+
39+
The *DndHandler* class handles drag-and-drop events tracking Motion and
40+
ButtonRelease events on the root of the event widget.
41+
42+
.. method:: cancel(event=None)
43+
44+
Cancel the drag-and-drop process.
45+
46+
.. method:: finish(event, commit=0)
47+
48+
Execute end of drag-and-drop functions.
49+
50+
.. method:: on_motion(event)
51+
52+
Inspect area below mouse for target objects while drag is performed.
53+
54+
.. method:: on_release(event)
55+
56+
Signal end of drag when the release pattern is triggered.
57+
58+
.. function:: dnd_start(source, event)
59+
60+
Factory function for drag-and-drop process.
61+
62+
.. seealso::
63+
64+
:ref:`Bindings-and-Events`

0 commit comments

Comments
 (0)