1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
|
.. wxPython Phoenix documentation
This file was generated by Phoenix's sphinx generator and associated
tools, do not edit by hand.
Copyright: (c) 2011-2020 by Total Control Software
License: wxWindows License
.. include:: headings.inc
.. _wx.FileDialog:
==========================================================================================================================================
|phoenix_title| **wx.FileDialog**
==========================================================================================================================================
This class represents the file chooser dialog.
The path and filename are distinct elements of a full file pathname. If path is :meth:`~wx.FileDialog.EmptyString`, the current directory will be used. If filename is :meth:`~wx.FileDialog.EmptyString`, no default filename will be supplied. The wildcard determines what files are displayed in the file selector, and file extension supplies a type extension for the required filename.
The typical usage for the open file dialog is: ::
def OnOpen(self, event):
if self.contentNotSaved:
if wx.MessageBox("Current content has not been saved! Proceed?", "Please confirm",
wx.ICON_QUESTION | wx.YES_NO, self) == wx.NO:
return
# otherwise ask the user what new file to open
with wx.FileDialog(self, "Open XYZ file", wildcard="XYZ files (*.xyz)|*.xyz",
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:
if fileDialog.ShowModal() == wx.ID_CANCEL:
return # the user changed their mind
# Proceed loading the file chosen by the user
pathname = fileDialog.GetPath()
try:
with open(pathname, 'r') as file:
self.doLoadDataOrWhatever(file)
except IOError:
wx.LogError("Cannot open file '%s'." % newfile)
The typical usage for the save file dialog is instead somewhat simpler: ::
def OnSaveAs(self, event):
with wx.FileDialog(self, "Save XYZ file", wildcard="XYZ files (*.xyz)|*.xyz",
style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as fileDialog:
if fileDialog.ShowModal() == wx.ID_CANCEL:
return # the user changed their mind
# save the current contents in the file
pathname = fileDialog.GetPath()
try:
with open(pathname, 'w') as file:
self.doSaveData(file)
except IOError:
wx.LogError("Cannot save current data in file '%s'." % pathname)
|phoenix_title| Wildcard Filters
================================
All implementations of the :ref:`wx.FileDialog` provide a wildcard filter. Typing a filename containing wildcards (, ?) in the filename text item, and clicking on Ok, will result in only those files matching the pattern being displayed. The wildcard may be a specification for multiple types of file with a description for each, such as: ::
wildcard = "BMP and GIF files (*.bmp;*.gif)|*.bmp;*.gif|PNG files (*.png)|*.png"
On Mac macOS in the open file dialog the filter choice box is not shown by default. Instead all given wildcards are applied at the same time: So in the above example all bmp, gif and png files are displayed. To enforce the display of the filter choice set the corresponding :ref:`wx.SystemOptions` before calling the file open dialog: ::
wx.SystemOptions.SetOption(wx.OSX_FILEDIALOG_ALWAYS_SHOW_TYPES, 1)
But in contrast to Windows and Unix, where the file type choice filters only the selected files, on Mac macOS even in this case the dialog shows all files matching all file types. The files which does not match the currently selected file type are greyed out and are not selectable.
|phoenix_title| Dialog Customization
====================================
.. _FileDialog-styles:
|styles| Window Styles
================================
Uniquely among the other standard dialogs, :ref:`wx.FileDialog` can be customized by adding extra controls to it. Moreover, there are two ways to do it: the first one is to define a callback function and use :meth:`~wx.FileDialog.SetExtraControlCreator` to tell the dialog to call it, while the second one requires defining a class inheriting from :ref:`wx.FileDialogCustomizeHook` and implementing its virtual functions, notably :meth:`wx.FileDialogCustomizeHook.AddCustomControls` where the extra controls have to be created, and finally calling :meth:`~wx.FileDialog.SetCustomizeHook` with this custom hook object. The first approach is somewhat simpler and more flexible, as it allows to create any kind of custom controls, but is not supported by the "new style" (where "new" means used since Windows Vista, i.e. circa 2007) file dialogs under MSW. Because of this, calling :meth:`~wx.FileDialog.SetExtraControlCreator` in wxMSW forces the use of old style (Windows XP) dialogs, that may look out of place. The second approach is implemented by the MSW dialogs natively and doesn't suffer from this limitation, so its use is recommended, especially if the few simple control types supported by it (see :ref:`wx.FileDialogCustomize` for more information about the supported controls) are sufficient for your needs. Both of the approaches to the dialog customization are demonstrated in the :ref:`Dialogs Sample <dialogs sample>`, please check it for more details. ^^ This class supports the following styles:
- ``wx.FD_DEFAULT_STYLE``: Equivalent to ``FD_OPEN`` .
- ``wx.FD_OPEN``: This is an open dialog; usually this means that the default button's label of the dialog is "Open". Cannot be combined with ``FD_SAVE`` .
- ``wx.FD_SAVE``: This is a save dialog; usually this means that the default button's label of the dialog is "Save". Cannot be combined with ``FD_OPEN`` .
- ``wx.FD_OVERWRITE_PROMPT``: For save dialog only: prompt for a confirmation if a file will be overwritten. This style is always enabled on wxOSX and cannot be disabled.
- ``wx.FD_NO_FOLLOW``: Directs the dialog to return the path and file name of the selected shortcut file, not its target as it does by default. Currently this flag is only implemented in wxMSW and wxOSX (where it prevents aliases from being resolved). The non-dereferenced link path is always returned, even without this flag, under Unix and so using it there doesn't do anything. This flag was added in wxWidgets 3.1.0.
- ``wx.FD_FILE_MUST_EXIST``: For open dialog only: the user may only select files that actually exist. Notice that under macOS the file dialog with ``FD_OPEN`` style always behaves as if this style was specified, because it is impossible to choose a file that doesn't exist from a standard macOS file dialog.
- ``wx.FD_MULTIPLE``: For open dialog only: allows selecting multiple files.
- ``wx.FD_CHANGE_DIR``: Change the current working directory (when the dialog is dismissed) to the directory where the file(s) chosen by the user are.
- ``wx.FD_PREVIEW``: Show the preview of the selected files (currently only supported by wxGTK).
- ``wx.FD_SHOW_HIDDEN``: Show hidden files. This flag was added in wxWidgets 3.1.3 ^^
.. seealso:: :ref:`FileDialog Overview <filedialog overview>`, :ref:`wx.FileSelector`
|
|class_hierarchy| Class Hierarchy
=================================
.. raw:: html
<div id="toggleBlock" onclick="return toggleVisibility(this)" class="closed" style="cursor:pointer;">
<img id="toggleBlock-trigger" src="_static/images/closed.png"/>
Inheritance diagram for class <strong>FileDialog</strong>:
</div>
<div id="toggleBlock-summary" style="display:block;"></div>
<div id="toggleBlock-content" style="display:none;">
<p class="graphviz">
<center><img src="_static/images/inheritance/wx.FileDialog_inheritance.png" alt="Inheritance diagram of FileDialog" usemap="#dummy" class="inheritance"/></center>
<script type="text/javascript">toggleVisibilityOnLoad(document.getElementById('toggleBlock'))</script>
<map id="dummy" name="dummy"> <area shape="rect" id="node1" href="wx.FileDialog.html" title="wx.FileDialog" alt="" coords="54,469,163,499"/> <area shape="rect" id="node2" href="wx.Dialog.html" title="wx.Dialog" alt="" coords="65,392,152,421"/> <area shape="rect" id="node3" href="wx.TopLevelWindow.html" title="wx.TopLevelWindow" alt="" coords="31,315,186,344"/> <area shape="rect" id="node4" href="wx.NonOwnedWindow.html" title="wx.NonOwnedWindow" alt="" coords="25,237,193,267"/> <area shape="rect" id="node5" href="wx.Window.html" title="wx.Window" alt="" coords="60,160,157,189"/> <area shape="rect" id="node6" href="wx.EvtHandler.html" title="wx.EvtHandler" alt="" coords="50,83,167,112"/> <area shape="rect" id="node7" href="wx.Object.html" title="wx.Object" alt="" coords="5,5,92,35"/> <area shape="rect" id="node8" href="wx.Trackable.html" title="wx.Trackable" alt="" coords="117,5,223,35"/> </map>
</p>
</div>
|
|method_summary| Methods Summary
================================
================================================================================ ================================================================================
:meth:`~wx.FileDialog.__init__` Constructor.
:meth:`~wx.FileDialog.GetClassDefaultAttributes`
:meth:`~wx.FileDialog.GetCurrentlySelectedFilename` Returns the path of the file currently selected in dialog.
:meth:`~wx.FileDialog.GetCurrentlySelectedFilterIndex` Returns the file type filter index currently selected in dialog.
:meth:`~wx.FileDialog.GetDirectory` Returns the default directory.
:meth:`~wx.FileDialog.GetExtraControl` If functions :meth:`~FileDialog.SetExtraControlCreator` and :meth:`~FileDialog.ShowModal` were called, returns the extra window.
:meth:`~wx.FileDialog.GetFilename` Returns the default filename.
:meth:`~wx.FileDialog.GetFilenames` Returns a list of filenames chosen in the dialog. This function
:meth:`~wx.FileDialog.GetFilterIndex` Returns the index into the list of filters supplied, optionally, in the wildcard parameter.
:meth:`~wx.FileDialog.GetMessage` Returns the message that will be displayed on the dialog.
:meth:`~wx.FileDialog.GetPath` Returns the full path (directory and filename) of the selected file.
:meth:`~wx.FileDialog.GetPaths` Returns a list of the full paths of the files chosen. This function
:meth:`~wx.FileDialog.GetWildcard` Returns the file dialog wildcard.
:meth:`~wx.FileDialog.SetCustomizeHook` Set the hook to be used for customizing the dialog contents.
:meth:`~wx.FileDialog.SetDirectory` Sets the default directory.
:meth:`~wx.FileDialog.SetFilename` Sets the default filename.
:meth:`~wx.FileDialog.SetFilterIndex` Sets the default filter index, starting from zero.
:meth:`~wx.FileDialog.SetMessage` Sets the message that will be displayed on the dialog.
:meth:`~wx.FileDialog.SetPath` Sets the path (the combined directory and filename that will be returned when the dialog is dismissed).
:meth:`~wx.FileDialog.SetWildcard` Sets the wildcard, which can contain multiple file types, for example: "``BMP`` files (.bmp)|.bmp|GIF files (.gif)|.gif".
:meth:`~wx.FileDialog.ShowModal` Shows the dialog, returning ``ID_OK`` if the user pressed ``wx.OK``, and ``ID_CANCEL`` otherwise.
================================================================================ ================================================================================
|
|property_summary| Properties Summary
=====================================
================================================================================ ================================================================================
:attr:`~wx.FileDialog.CurrentlySelectedFilename` See :meth:`~wx.FileDialog.GetCurrentlySelectedFilename`
:attr:`~wx.FileDialog.CurrentlySelectedFilterIndex` See :meth:`~wx.FileDialog.GetCurrentlySelectedFilterIndex`
:attr:`~wx.FileDialog.Directory` See :meth:`~wx.FileDialog.GetDirectory` and :meth:`~wx.FileDialog.SetDirectory`
:attr:`~wx.FileDialog.ExtraControl` See :meth:`~wx.FileDialog.GetExtraControl`
:attr:`~wx.FileDialog.Filename` See :meth:`~wx.FileDialog.GetFilename` and :meth:`~wx.FileDialog.SetFilename`
:attr:`~wx.FileDialog.Filenames` See :meth:`~wx.FileDialog.GetFilenames`
:attr:`~wx.FileDialog.FilterIndex` See :meth:`~wx.FileDialog.GetFilterIndex` and :meth:`~wx.FileDialog.SetFilterIndex`
:attr:`~wx.FileDialog.Message` See :meth:`~wx.FileDialog.GetMessage` and :meth:`~wx.FileDialog.SetMessage`
:attr:`~wx.FileDialog.Path` See :meth:`~wx.FileDialog.GetPath` and :meth:`~wx.FileDialog.SetPath`
:attr:`~wx.FileDialog.Paths` See :meth:`~wx.FileDialog.GetPaths`
:attr:`~wx.FileDialog.Wildcard` See :meth:`~wx.FileDialog.GetWildcard` and :meth:`~wx.FileDialog.SetWildcard`
================================================================================ ================================================================================
|
|api| Class API
===============
.. class:: wx.FileDialog(Dialog)
**Possible constructors**::
FileDialog(parent, message=FileSelectorPromptStr,
defaultDir=EmptyString, defaultFile=EmptyString,
wildcard=FileSelectorDefaultWildcardStr, style=FD_DEFAULT_STYLE,
pos=DefaultPosition, size=DefaultSize, name=FileDialogNameStr)
This class represents the file chooser dialog.
.. method:: __init__(self, parent, message=FileSelectorPromptStr, defaultDir=EmptyString, defaultFile=EmptyString, wildcard=FileSelectorDefaultWildcardStr, style=FD_DEFAULT_STYLE, pos=DefaultPosition, size=DefaultSize, name=FileDialogNameStr)
Constructor.
Use :meth:`ShowModal` to show the dialog.
:param `parent`: Parent window.
:type `parent`: wx.Window
:param `message`: Message to show on the dialog.
:type `message`: string
:param `defaultDir`: The default directory, or the empty string.
:type `defaultDir`: string
:param `defaultFile`: The default filename, or the empty string.
:type `defaultFile`: string
:param `wildcard`: A wildcard, such as "x.x" or "``BMP`` files (.bmp)|.bmp|GIF files (.gif)|.gif". Note that the native Motif dialog has some limitations with respect to wildcards; see the Remarks section above.
:type `wildcard`: string
:param `style`: A dialog style. See ``FD_*`` styles for more info.
:type `style`: long
:param `pos`: Dialog position. Not implemented.
:type `pos`: wx.Point
:param `size`: Dialog size. Not implemented.
:type `size`: wx.Size
:param `name`: Dialog name. Not implemented.
:type `name`: string
.. staticmethod:: GetClassDefaultAttributes(variant=WINDOW_VARIANT_NORMAL)
:param `variant`:
:type `variant`: wx.WindowVariant
:rtype: :ref:`wx.VisualAttributes`
.. method:: GetCurrentlySelectedFilename(self)
Returns the path of the file currently selected in dialog.
Notice that this file is not necessarily going to be accepted by the user, so calling this function mostly makes sense from an update UI event handler of a custom file dialog extra control to update its state depending on the currently selected file.
Currently this function is fully implemented under GTK and MSW and always returns an empty string elsewhere.
:rtype: `string`
:returns:
The path of the currently selected file or an empty string if nothing is selected.
.. versionadded:: 2.9.5
.. seealso:: :meth:`SetExtraControlCreator`
.. method:: GetCurrentlySelectedFilterIndex(self)
Returns the file type filter index currently selected in dialog.
Notice that this file type filter is not necessarily going to be the one finally accepted by the user, so calling this function mostly makes sense from an update UI event handler of a custom file dialog extra control to update its state depending on the currently selected file type filter.
Currently this function is fully implemented under macOS and MSW and always returns ``NOT_FOUND`` elsewhere.
:rtype: `int`
:returns:
The 0-based index of the currently selected file type filter or ``wx.NOT_FOUND`` if nothing is selected.
.. versionadded:: 4.1/wxWidgets-3.1.3
- MSW
.. versionadded:: 4.1/wxWidgets-3.1.5
- macOS
.. seealso:: :meth:`SetExtraControlCreator`
.. seealso:: :meth:`GetFilterIndex`
.. seealso:: :meth:`SetFilterIndex`
.. method:: GetDirectory(self)
Returns the default directory.
:rtype: `string`
.. method:: GetExtraControl(self)
If functions :meth:`SetExtraControlCreator` and :meth:`ShowModal` were called, returns the extra window.
Otherwise returns ``None``.
:rtype: :ref:`wx.Window`
.. versionadded:: 2.9.0
.. method:: GetFilename(self)
Returns the default filename.
:rtype: `string`
.. note::
This function can't be used with dialogs which have the ``FD_MULTIPLE`` style, use :meth:`GetFilenames` instead.
.. method:: GetFilenames(self)
Returns a list of filenames chosen in the dialog. This function
should only be used with the dialogs which have wx.``MULTIPLE`` style,
use GetFilename for the others.
:rtype: `list of strings`
.. method:: GetFilterIndex(self)
Returns the index into the list of filters supplied, optionally, in the wildcard parameter.
Before the dialog is shown, this is the index which will be used when the dialog is first displayed.
After the dialog is shown, this is the index selected by the user.
:rtype: `int`
.. method:: GetMessage(self)
Returns the message that will be displayed on the dialog.
:rtype: `string`
.. method:: GetPath(self)
Returns the full path (directory and filename) of the selected file.
:rtype: `string`
.. note::
This function can't be used with dialogs which have the ``FD_MULTIPLE`` style, use :meth:`GetPaths` instead.
.. method:: GetPaths(self)
Returns a list of the full paths of the files chosen. This function
should only be used with the dialogs which have wx.``MULTIPLE`` style, use
GetPath for the others.
:rtype: `list of strings`
.. method:: GetWildcard(self)
Returns the file dialog wildcard.
:rtype: `string`
.. method:: SetCustomizeHook(self, customizeHook)
Set the hook to be used for customizing the dialog contents.
This function can be called before calling :meth:`ShowModal` to specify that the dialog contents should be customized using the provided hook. See :ref:`wx.FileDialogCustomizeHook` documentation and :ref:`Dialogs Sample <dialogs sample>` for the examples of using it.
:param `customizeHook`: The hook object that will be used by the dialog. This object must remain valid at least until :meth:`ShowModal` returns.
:type `customizeHook`: wx.FileDialogCustomizeHook
:rtype: `bool`
:returns:
``True`` if the hook was successfully set or ``False`` if customizing the file dialog is not supported by the current platform.
.. versionadded:: 4.1/wxWidgets-3.1.7
.. note::
In order to define a custom hook object, :ref:`/filedlgcustomize.h` must be included in addition to the usual :ref:`/filedlg.h` header.
.. method:: SetDirectory(self, directory)
Sets the default directory.
:param `directory`:
:type `directory`: string
.. method:: SetFilename(self, setfilename)
Sets the default filename.
In wxGTK this will have little effect unless a default directory has previously been set.
:param `setfilename`:
:type `setfilename`: string
.. method:: SetFilterIndex(self, filterIndex)
Sets the default filter index, starting from zero.
:param `filterIndex`:
:type `filterIndex`: int
.. method:: SetMessage(self, message)
Sets the message that will be displayed on the dialog.
:param `message`:
:type `message`: string
.. method:: SetPath(self, path)
Sets the path (the combined directory and filename that will be returned when the dialog is dismissed).
:param `path`:
:type `path`: string
.. method:: SetWildcard(self, wildCard)
Sets the wildcard, which can contain multiple file types, for example: "``BMP`` files (.bmp)|.bmp|GIF files (.gif)|.gif".
Note that the native Motif dialog has some limitations with respect to wildcards; see the Remarks section above.
:param `wildCard`:
:type `wildCard`: string
.. method:: ShowModal(self)
Shows the dialog, returning ``ID_OK`` if the user pressed ``wx.OK``, and ``ID_CANCEL`` otherwise.
:rtype: `int`
.. attribute:: CurrentlySelectedFilename
See :meth:`~wx.FileDialog.GetCurrentlySelectedFilename`
.. attribute:: CurrentlySelectedFilterIndex
See :meth:`~wx.FileDialog.GetCurrentlySelectedFilterIndex`
.. attribute:: Directory
See :meth:`~wx.FileDialog.GetDirectory` and :meth:`~wx.FileDialog.SetDirectory`
.. attribute:: ExtraControl
See :meth:`~wx.FileDialog.GetExtraControl`
.. attribute:: Filename
See :meth:`~wx.FileDialog.GetFilename` and :meth:`~wx.FileDialog.SetFilename`
.. attribute:: Filenames
See :meth:`~wx.FileDialog.GetFilenames`
.. attribute:: FilterIndex
See :meth:`~wx.FileDialog.GetFilterIndex` and :meth:`~wx.FileDialog.SetFilterIndex`
.. attribute:: Message
See :meth:`~wx.FileDialog.GetMessage` and :meth:`~wx.FileDialog.SetMessage`
.. attribute:: Path
See :meth:`~wx.FileDialog.GetPath` and :meth:`~wx.FileDialog.SetPath`
.. attribute:: Paths
See :meth:`~wx.FileDialog.GetPaths`
.. attribute:: Wildcard
See :meth:`~wx.FileDialog.GetWildcard` and :meth:`~wx.FileDialog.SetWildcard`
|