]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/FileDialog_Save.py
xcode 1.2 native targets
[wxWidgets.git] / wxPython / demo / FileDialog_Save.py
1
2 import os
3 import wx
4
5 #---------------------------------------------------------------------------
6
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|" \
13 "All files (*.*)|*.*"
14
15 def runTest(frame, nb, log):
16 log.WriteText("CWD: %s\n" % os.getcwd())
17
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.
21 #
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.
25 dlg = wx.FileDialog(
26 frame, message="Save file as ...", defaultDir=os.getcwd(),
27 defaultFile="", wildcard=wildcard, style=wx.SAVE
28 )
29
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.
32 dlg.SetFilterIndex(2)
33
34 # Show the dialog and retrieve the user response. If it is the OK response,
35 # process the data.
36 if dlg.ShowModal() == wx.ID_OK:
37 path = dlg.GetPath()
38 log.WriteText('You selected "%s"' % path)
39
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.
43 #
44 # The code to do so would be similar to this, assuming 'data' contains
45 # the data you want to save:
46 #
47 # fp = file(path, 'w') # Create file anew
48 # fp.write(data)
49 # fp.close()
50 #
51 # You might want to add some error checking :-)
52 #
53
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())
57
58 # Destroy the dialog. Don't do this until you are done with it!
59 # BAD things can happen otherwise!
60 dlg.Destroy()
61
62 #---------------------------------------------------------------------------
63
64
65 overview = """\
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.
70
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.
74
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.'
79 Yum!
80 """
81
82
83 if __name__ == '__main__':
84 import sys,os
85 import run
86 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
87