7
7
"""
8
8
9
9
import wx
10
+ import os
11
+
12
+ #---------------------------------------------------------------------------
13
+
14
+ # This is how you pre-establish a file filter so that the dialog
15
+ # only shows the extension(s) you want it to.
16
+ wildcard = "Python source (*.py)|*.py|" \
17
+ "Compiled Python (*.pyc)|*.pyc|" \
18
+ "SPAM files (*.spam)|*.spam|" \
19
+ "Egg file (*.egg)|*.egg|" \
20
+ "All files (*.*)|*.*"
21
+
22
+ #---------------------------------------------------------------------------
10
23
11
24
12
25
class AppLogic (object ):
@@ -19,6 +32,7 @@ class AppLogic(object):
19
32
In a real app, this would be a substantial collection of
20
33
modules, classes, etc...
21
34
"""
35
+
22
36
def file_open (self , filename = "default_name" ):
23
37
"""This method opens a file"""
24
38
print "Open a file: "
@@ -30,6 +44,9 @@ def file_close(self):
30
44
print "I'd be closing a file now"
31
45
32
46
47
+
48
+
49
+
33
50
class TestFrame (wx .Frame ):
34
51
def __init__ (self , app_logic , * args , ** kwargs ):
35
52
kwargs .setdefault ('title' , "Simple test App" )
@@ -64,19 +81,103 @@ def __init__(self, app_logic, *args, **kwargs):
64
81
def onButton (self , evt = None ):
65
82
print "You pushed the button!"
66
83
67
- def onOpen (self , evt = None ):
68
- print "open menu selected"
69
- self .app_logic .file_open ()
70
-
71
- def onClose (self , evt = None ):
72
- print "close menu selected"
73
- self .app_logic .file_close ()
74
84
75
85
def onExit (self , evt = None ):
76
86
print "Exit the program here"
77
87
print "The event passed to onExit is type " , type (evt ),
78
88
self .Close ()
79
89
90
+ def onClose (self , evt = None ):
91
+ print "close menu selected"
92
+ self .app_logic .file_close ()
93
+
94
+
95
+ def onNew ( self , evt = None ):
96
+ """This method creates a new file"""
97
+
98
+ # Create the dialog. In this case the current directory is forced as the starting
99
+ # directory for the dialog, and no default file name is forced. This can easilly
100
+ # be changed in your program. This is an 'save' dialog.
101
+ #
102
+ # Unlike the 'open dialog' example found elsewhere, this example does NOT
103
+ # force the current working directory to change if the user chooses a different
104
+ # directory than the one initially set.
105
+ dlg = wx .FileDialog (
106
+ self , message = "Save file as ..." , defaultDir = os .getcwd (),
107
+ defaultFile = "" , wildcard = wildcard , style = wx .SAVE )
108
+
109
+ # This sets the default filter that the user will initially see. Otherwise,
110
+ # the first filter in the list will be used by default.
111
+ dlg .SetFilterIndex (2 )
112
+
113
+ # Show the dialog and retrieve the user response. If it is the OK response,
114
+ # process the data.
115
+ if dlg .ShowModal () == wx .ID_OK :
116
+ path = dlg .GetPath ()
117
+ print "In onNew, the path is %s" % path
118
+ # Normally, at this point you would save your data using the file and path
119
+ # data that the user provided to you, but since we didn't actually start
120
+ # with any data to work with, that would be difficult.
121
+ #
122
+ # The code to do so would be similar to this, assuming 'data' contains
123
+ # the data you want to save:
124
+ #
125
+ # fp = file(path, 'w') # Create file anew
126
+ # fp.write(data)
127
+ # fp.close()
128
+ #
129
+ # You might want to add some error checking :-)
130
+ #
131
+ else :
132
+ print "The file dialog was canceled before anything was selected"
133
+
134
+ # Note that the current working dir didn't change. This is good since
135
+ # that's the way we set it up.
136
+
137
+ # Destroy the dialog. Don't do this until you are done with it!
138
+ # BAD things can happen otherwise!
139
+ dlg .Destroy ()
140
+
141
+
142
+
143
+
144
+ def onOpen (self , evt = None ):
145
+ """This method opens an existing file"""
146
+ print "Open a file: "
147
+ # Create the dialog. In this case the current directory is forced as the starting
148
+ # directory for the dialog, and no default file name is forced. This can easilly
149
+ # be changed in your program. This is an 'open' dialog, and allows multitple
150
+ # file selections as well.
151
+ #
152
+ # Finally, if the directory is changed in the process of getting files, this
153
+ # dialog is set up to change the current working directory to the path chosen.
154
+ dlg = wx .FileDialog (
155
+ self , message = "Choose a file" ,
156
+ defaultDir = os .getcwd (),
157
+ defaultFile = "" ,
158
+ wildcard = wildcard ,
159
+ style = wx .OPEN | wx .CHANGE_DIR
160
+ )
161
+
162
+ # Show the dialog and retrieve the user response. If it is the OK response,
163
+ # process the data.
164
+ if dlg .ShowModal () == wx .ID_OK :
165
+ # This returns a Python list of files that were selected.
166
+ path = dlg .GetPath ()
167
+ print "I'd be opening file in onOpen " , path
168
+ self .app_logic .file_open ( path )
169
+ else :
170
+ print "The file dialog was canceled before anything was selected"
171
+
172
+
173
+
174
+
175
+ # Destroy the dialog. Don't do this until you are done with it!
176
+ # BAD things can happen otherwise!
177
+ dlg .Destroy ()
178
+
179
+
180
+
80
181
81
182
class TestApp (wx .App ):
82
183
def OnInit (self ):
0 commit comments