blob: dc82a974ce095d8937b9a908faeb8caa8ca7a7a3 [file] [log] [blame]
Nikhil80428ed2019-09-10 01:55:34 -07001Tkinter 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
15The :mod:`tkinter.simpledialog` module contains convenience classes and
16functions 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
53The :mod:`tkinter.filedialog` module provides classes and factory functions for
54creating file/directory selection windows.
55
56Native Load/Save Dialogs
57------------------------
58
59The following classes and functions provide file dialog windows that combine a
60native look-and-feel with configuration options to customize behaviour.
61The following keyword arguments are applicable to the classes and functions
62listed 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
Serhiy Storchaka138ccbb2019-11-12 16:57:03 +020076 | *multiple* - when true, selection of multiple items is allowed
Nikhil80428ed2019-09-10 01:55:34 -070077
78
79**Static factory functions**
80
81The below functions when called create a modal, native look-and-feel dialog,
82wait for the user's selection, then return the selected value(s) or ``None`` to the
83caller.
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
119The below classes are used for creating file/directory windows from scratch.
120These 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
218The :mod:`tkinter.commondialog` module provides the :class:`Dialog` class that
219is 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
Serhiy Storchaka138ccbb2019-11-12 16:57:03 +0200230 Modules :mod:`tkinter.messagebox`, :ref:`tut-files`