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