]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/FileDialog_Save.py
fixes for unicode build, return values from event handlers (via
[wxWidgets.git] / wxPython / demo / FileDialog_Save.py
CommitLineData
1fded56b 1
8fa876ca
RD
2import os
3import wx
1fded56b
RD
4
5#---------------------------------------------------------------------------
6
8fa876ca 7# This is how you pre-establish a file filter so that the dialog
95bfd958 8# only shows the extension(s) you want it to.
1fded56b
RD
9wildcard = "Python source (*.py)|*.py|" \
10 "Compiled Python (*.pyc)|*.pyc|" \
11 "SPAM files (*.spam)|*.spam|" \
12 "Egg file (*.egg)|*.egg|" \
13 "All files (*.*)|*.*"
14
15def runTest(frame, nb, log):
16 log.WriteText("CWD: %s\n" % os.getcwd())
8fa876ca
RD
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.
1fded56b 32 dlg.SetFilterIndex(2)
8fa876ca
RD
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:
1fded56b
RD
37 path = dlg.GetPath()
38 log.WriteText('You selected "%s"' % path)
8fa876ca
RD
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.
1fded56b 56 log.WriteText("CWD: %s\n" % os.getcwd())
8fa876ca
RD
57
58 # Destroy the dialog. Don't do this until you are done with it!
59 # BAD things can happen otherwise!
1fded56b
RD
60 dlg.Destroy()
61
62#---------------------------------------------------------------------------
63
64
1fded56b 65overview = """\
8fa876ca
RD
66This class provides the file selection dialog. It incorporates OS-native features
67depending on the OS in use, and can be used both for open and save operations.
68The files displayed can be filtered by setting up a wildcard filter, multiple files
69can be selected (open only), and files can be forced in a read-only mode.
70
71There are two ways to get the results back from the dialog. GetFiles() returns only
72the file names themselves, in a Python list. GetPaths() returns the full path and
73filenames combined as a Python list.
74
95bfd958
RD
75One important thing to note: if you use the file extension filters, then files saved
76with the filter set to something will automatically get that extension appended to them
8fa876ca 77if it is not already there. For example, suppose the dialog was displaying the 'egg'
95bfd958 78extension and you entered a file name of 'fried'. It would be saved as 'fried.egg.'
8fa876ca 79Yum!
1fded56b
RD
80"""
81
82
83if __name__ == '__main__':
84 import sys,os
85 import run
8eca4fef 86 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
1fded56b 87