]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/FileDialog.py
added missing button state
[wxWidgets.git] / wxPython / demo / FileDialog.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 #---------------------------------------------------------------------------
16
17 class 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(
40 self, message="Choose a file", defaultDir=os.getcwd(),
41 defaultFile="", wildcard=wildcard, style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR
42 )
43
44 # Show the dialog and retrieve the user response. If it is the OK response,
45 # process the data.
46 if dlg.ShowModal() == wx.ID_OK:
47 # This returns a Python list of files that were selected.
48 paths = dlg.GetPaths()
49
50 self.log.WriteText('You selected %d files:' % len(paths))
51
52 for path in paths:
53 self.log.WriteText(' %s\n' % path)
54
55 # Compare this with the debug above; did we change working dirs?
56 self.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 def OnButton2(self, evt):
65 self.log.WriteText("CWD: %s\n" % os.getcwd())
66
67 # Create the dialog. In this case the current directory is forced as the starting
68 # directory for the dialog, and no default file name is forced. This can easilly
69 # be changed in your program. This is an 'save' dialog.
70 #
71 # Unlike the 'open dialog' example found elsewhere, this example does NOT
72 # force the current working directory to change if the user chooses a different
73 # directory than the one initially set.
74 dlg = wx.FileDialog(
75 self, message="Save file as ...", defaultDir=os.getcwd(),
76 defaultFile="", wildcard=wildcard, style=wx.SAVE
77 )
78
79 # This sets the default filter that the user will initially see. Otherwise,
80 # the first filter in the list will be used by default.
81 dlg.SetFilterIndex(2)
82
83 # Show the dialog and retrieve the user response. If it is the OK response,
84 # process the data.
85 if dlg.ShowModal() == wx.ID_OK:
86 path = dlg.GetPath()
87 self.log.WriteText('You selected "%s"' % path)
88
89 # Normally, at this point you would save your data using the file and path
90 # data that the user provided to you, but since we didn't actually start
91 # with any data to work with, that would be difficult.
92 #
93 # The code to do so would be similar to this, assuming 'data' contains
94 # the data you want to save:
95 #
96 # fp = file(path, 'w') # Create file anew
97 # fp.write(data)
98 # fp.close()
99 #
100 # You might want to add some error checking :-)
101 #
102
103 # Note that the current working dir didn't change. This is good since
104 # that's the way we set it up.
105 self.log.WriteText("CWD: %s\n" % os.getcwd())
106
107 # Destroy the dialog. Don't do this until you are done with it!
108 # BAD things can happen otherwise!
109 dlg.Destroy()
110
111
112
113 #---------------------------------------------------------------------------
114
115
116 def runTest(frame, nb, log):
117 win = TestPanel(nb, log)
118 return win
119
120 #---------------------------------------------------------------------------
121
122
123 overview = """\
124 This class provides the file selection dialog. It incorporates OS-native features
125 depending on the OS in use, and can be used both for open and save operations.
126 The files displayed can be filtered by setting up a wildcard filter, multiple files
127 can be selected (open only), and files can be forced in a read-only mode.
128
129 There are two ways to get the results back from the dialog. GetFiles() returns only
130 the file names themselves, in a Python list. GetPaths() returns the full path and
131 filenames combined as a Python list.
132
133 """
134
135
136 if __name__ == '__main__':
137 import sys,os
138 import run
139 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
140