Skip to content

Commit 3b296ea

Browse files
committed
Added file dialog boxes
1 parent 8138c81 commit 3b296ea

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

week-09/code/basic_app_5.py

+48-2
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):
@@ -72,8 +85,41 @@ def __init__(self, app_logic, *args, **kwargs):
7285

7386

7487
def onOpen(self, evt=None):
75-
print "open menu selected"
76-
self.app_logic.file_open()
88+
"""This method opens an existing file"""
89+
print "Open a file: "
90+
# Create the dialog. In this case the current directory is forced as the starting
91+
# directory for the dialog, and no default file name is forced. This can easilly
92+
# be changed in your program. This is an 'open' dialog, and allows multitple
93+
# file selections as well.
94+
#
95+
# Finally, if the directory is changed in the process of getting files, this
96+
# dialog is set up to change the current working directory to the path chosen.
97+
dlg = wx.FileDialog(
98+
self, message="Choose a file",
99+
defaultDir=os.getcwd(),
100+
defaultFile="",
101+
wildcard=wildcard,
102+
style=wx.OPEN | wx.CHANGE_DIR
103+
)
104+
105+
# Show the dialog and retrieve the user response. If it is the OK response,
106+
# process the data.
107+
if dlg.ShowModal() == wx.ID_OK:
108+
# This returns a Python list of files that were selected.
109+
path = dlg.GetPath()
110+
print "I'd be opening file in onOpen ", path
111+
self.app_logic.file_open( path )
112+
else :
113+
print "The file dialog was canceled before anything was selected"
114+
115+
116+
117+
118+
# Destroy the dialog. Don't do this until you are done with it!
119+
# BAD things can happen otherwise!
120+
dlg.Destroy()
121+
122+
77123

78124
def onClose(self, evt=None):
79125
print "close menu selected"

0 commit comments

Comments
 (0)