]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/FileDialog.py
removed WXWIN_COMPATIBILITY_2_4 from common and wxMSW files (patch 1675546)
[wxWidgets.git] / wxPython / demo / FileDialog.py
CommitLineData
cf694132 1
8fa876ca
RD
2import os
3import wx
cf694132
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.
34a544a6 9wildcard = "Python source (*.py)|*.py|" \
eb0f373c 10 "Compiled Python (*.pyc)|*.pyc|" \
34a544a6
RD
11 "SPAM files (*.spam)|*.spam|" \
12 "Egg file (*.egg)|*.egg|" \
eb0f373c
RD
13 "All files (*.*)|*.*"
14
34a544a6 15#---------------------------------------------------------------------------
8fa876ca 16
34a544a6
RD
17class TestPanel(wx.Panel):
18 def __init__(self, parent, log):
19 self.log = log
20 wx.Panel.__init__(self, parent, -1)
21
22 b = wx.Button(self, -1, "Create and Show an OPEN FileDialog", (50,50))
23 self.Bind(wx.EVT_BUTTON, self.OnButton, b)
24
25 b = wx.Button(self, -1, "Create and Show a SAVE FileDialog", (50,90))
26 self.Bind(wx.EVT_BUTTON, self.OnButton2, b)
27
28
29 def OnButton(self, evt):
30 self.log.WriteText("CWD: %s\n" % os.getcwd())
31
32 # Create the dialog. In this case the current directory is forced as the starting
33 # directory for the dialog, and no default file name is forced. This can easilly
34 # be changed in your program. This is an 'open' dialog, and allows multitple
35 # file selections as well.
36 #
37 # Finally, if the directory is changed in the process of getting files, this
38 # dialog is set up to change the current working directory to the path chosen.
39 dlg = wx.FileDialog(
19a88534
RD
40 self, message="Choose a file",
41 defaultDir=os.getcwd(),
42 defaultFile="",
43 wildcard=wildcard,
44 style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR
34a544a6
RD
45 )
46
47 # Show the dialog and retrieve the user response. If it is the OK response,
48 # process the data.
49 if dlg.ShowModal() == wx.ID_OK:
50 # This returns a Python list of files that were selected.
51 paths = dlg.GetPaths()
52
53 self.log.WriteText('You selected %d files:' % len(paths))
54
55 for path in paths:
56 self.log.WriteText(' %s\n' % path)
57
58 # Compare this with the debug above; did we change working dirs?
59 self.log.WriteText("CWD: %s\n" % os.getcwd())
60
61 # Destroy the dialog. Don't do this until you are done with it!
62 # BAD things can happen otherwise!
63 dlg.Destroy()
64
65
66
67 def OnButton2(self, evt):
68 self.log.WriteText("CWD: %s\n" % os.getcwd())
69
70 # Create the dialog. In this case the current directory is forced as the starting
71 # directory for the dialog, and no default file name is forced. This can easilly
72 # be changed in your program. This is an 'save' dialog.
73 #
74 # Unlike the 'open dialog' example found elsewhere, this example does NOT
75 # force the current working directory to change if the user chooses a different
76 # directory than the one initially set.
77 dlg = wx.FileDialog(
78 self, message="Save file as ...", defaultDir=os.getcwd(),
79 defaultFile="", wildcard=wildcard, style=wx.SAVE
80 )
81
82 # This sets the default filter that the user will initially see. Otherwise,
83 # the first filter in the list will be used by default.
84 dlg.SetFilterIndex(2)
85
86 # Show the dialog and retrieve the user response. If it is the OK response,
87 # process the data.
88 if dlg.ShowModal() == wx.ID_OK:
89 path = dlg.GetPath()
90 self.log.WriteText('You selected "%s"' % path)
91
92 # Normally, at this point you would save your data using the file and path
93 # data that the user provided to you, but since we didn't actually start
94 # with any data to work with, that would be difficult.
95 #
96 # The code to do so would be similar to this, assuming 'data' contains
97 # the data you want to save:
98 #
99 # fp = file(path, 'w') # Create file anew
100 # fp.write(data)
101 # fp.close()
102 #
103 # You might want to add some error checking :-)
104 #
105
106 # Note that the current working dir didn't change. This is good since
107 # that's the way we set it up.
108 self.log.WriteText("CWD: %s\n" % os.getcwd())
109
110 # Destroy the dialog. Don't do this until you are done with it!
111 # BAD things can happen otherwise!
112 dlg.Destroy()
8fa876ca 113
34a544a6
RD
114
115
116#---------------------------------------------------------------------------
8fa876ca 117
8fa876ca 118
34a544a6
RD
119def runTest(frame, nb, log):
120 win = TestPanel(nb, log)
121 return win
cf694132
RD
122
123#---------------------------------------------------------------------------
124
125
1fded56b 126overview = """\
8fa876ca
RD
127This class provides the file selection dialog. It incorporates OS-native features
128depending on the OS in use, and can be used both for open and save operations.
129The files displayed can be filtered by setting up a wildcard filter, multiple files
130can be selected (open only), and files can be forced in a read-only mode.
131
132There are two ways to get the results back from the dialog. GetFiles() returns only
133the file names themselves, in a Python list. GetPaths() returns the full path and
134filenames combined as a Python list.
cf694132 135
1fded56b 136"""
cf694132
RD
137
138
1fded56b
RD
139if __name__ == '__main__':
140 import sys,os
141 import run
8eca4fef 142 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
cf694132 143