]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/FileDialog_Save.py
   5 #--------------------------------------------------------------------------- 
   7 # This is how you pre-establish a file filter so that the dialog 
   8 # only shows the extension(s) you want it to. 
   9 wildcard 
= "Python source (*.py)|*.py|"     \
 
  10            "Compiled Python (*.pyc)|*.pyc|" \
 
  11            "SPAM files (*.spam)|*.spam|"    \
 
  12            "Egg file (*.egg)|*.egg|"        \
 
  15 def runTest(frame
, nb
, log
): 
  16     log
.WriteText("CWD: %s\n" % os
.getcwd()) 
  18     # Create the dialog. In this case the current directory is forced as the starting 
  19     # directory for the dialog, and no default file name is forced. This can easilly 
  20     # be changed in your program. This is an 'save' dialog. 
  22     # Unlike the 'open dialog' example found elsewhere, this example does NOT 
  23     # force the current working directory to change if the user chooses a different 
  24     # directory than the one initially set. 
  26         frame
, message
="Save file as ...", defaultDir
=os
.getcwd(),  
  27         defaultFile
="", wildcard
=wildcard
, style
=wx
.SAVE
 
  30     # This sets the default filter that the user will initially see. Otherwise, 
  31     # the first filter in the list will be used by default. 
  34     # Show the dialog and retrieve the user response. If it is the OK response,  
  36     if dlg
.ShowModal() == wx
.ID_OK
: 
  38         log
.WriteText('You selected "%s"' % path
) 
  40         # Normally, at this point you would save your data using the file and path 
  41         # data that the user provided to you, but since we didn't actually start 
  42         # with any data to work with, that would be difficult. 
  44         # The code to do so would be similar to this, assuming 'data' contains 
  45         # the data you want to save: 
  47         # fp = file(path, 'w') # Create file anew 
  51         # You might want to add some error checking :-) 
  54     # Note that the current working dir didn't change. This is good since 
  55     # that's the way we set it up. 
  56     log
.WriteText("CWD: %s\n" % os
.getcwd()) 
  58     # Destroy the dialog. Don't do this until you are done with it! 
  59     # BAD things can happen otherwise! 
  62 #--------------------------------------------------------------------------- 
  66 This class provides the file selection dialog. It incorporates OS-native features 
  67 depending on the OS in use, and can be used both for open and save operations.  
  68 The files displayed can be filtered by setting up a wildcard filter, multiple files 
  69 can be selected (open only), and files can be forced in a read-only mode. 
  71 There are two ways to get the results back from the dialog. GetFiles() returns only 
  72 the file names themselves, in a Python list. GetPaths() returns the full path and  
  73 filenames combined as a Python list. 
  75 One important thing to note: if you use the file extension filters, then files saved 
  76 with the filter set to something will automatically get that extension appended to them 
  77 if it is not already there. For example, suppose the dialog was displaying the 'egg' 
  78 extension and you entered a file name of 'fried'. It would be saved as 'fried.egg.' 
  83 if __name__ 
== '__main__': 
  86     run
.main(['', os
.path
.basename(sys
.argv
[0])])