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