Skip to content

Commit 8138c81

Browse files
committed
Added file dialog boxes
1 parent 0e5c8c3 commit 8138c81

File tree

1 file changed

+108
-7
lines changed

1 file changed

+108
-7
lines changed

week-09/code/basic_app_4.py

+108-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@
77
"""
88

99
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+
#---------------------------------------------------------------------------
1023

1124

1225
class AppLogic(object):
@@ -19,6 +32,7 @@ class AppLogic(object):
1932
In a real app, this would be a substantial collection of
2033
modules, classes, etc...
2134
"""
35+
2236
def file_open(self, filename="default_name"):
2337
"""This method opens a file"""
2438
print "Open a file: "
@@ -30,6 +44,9 @@ def file_close(self):
3044
print "I'd be closing a file now"
3145

3246

47+
48+
49+
3350
class TestFrame(wx.Frame):
3451
def __init__(self, app_logic, *args, **kwargs):
3552
kwargs.setdefault('title', "Simple test App")
@@ -64,19 +81,103 @@ def __init__(self, app_logic, *args, **kwargs):
6481
def onButton(self, evt=None):
6582
print "You pushed the button!"
6683

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()
7484

7585
def onExit(self, evt=None):
7686
print "Exit the program here"
7787
print "The event passed to onExit is type ", type(evt),
7888
self.Close()
7989

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+
80181

81182
class TestApp(wx.App):
82183
def OnInit(self):

0 commit comments

Comments
 (0)