Sorry
to beat a dead horse again, but I want to make sure I understand
what's going on in the code below (Lines 4-9). And thanks to all
who’ve
already helped. . .
1 def OnOpen(self,e):
2 """ Open a file"""
3 self.dirname = ''
4 dlg = wx.FileDialog(self, "Choose a file",
self.dirname,
"", "*.*", wx.OPEN)
5 if dlg.ShowModal() == wx.ID_OK:
6 self.filename = dlg.GetFilename()
7 self.dirname = dlg.GetDirectory()
8 f = open(os.path.join(self.dirname,
self.filename), ‘r’)
9 self.control.SetValue(f.read())
10 f.close()
11 dlg.Destroy()
From my understanding:
Line 4 Opens the File Dialog Window. If the user chooses a
directory &
file, proceed to next line. . .
No Line for Creates a dialogue of type File Dialog.
Line 5 If user chooses a directory & file from the open dialog
window &
clicks OK. . .
This line shows the user the dialogue and waits for the user to
close the dialogue either by selecting a file, (with the option to
change directory to select one), and clicking OK or by clicking
CANCEL. If the user has done the former the indented code is
executed.
Line 6
This line stores the filename the user selected in varaible
self.filename?
Yes
Line 7
This line stores the directory chosen in variable self.dirname?
Yes
Line 8
Creates file object “F” which is the directory & file chosen
above?
No this line Opens the selected file in read mode,('r'), and saves
the file handle in a local variable f.
Line 9 Shows file object "F" in our text control box?
No this line reads the contents of f and then sets the value of
control, self.control, to the contents, which if compatible
will display the same to the user.
Lines 10,11 Obvious
Is this correct??? (Thanks in advance)
Nested functions, as denoted by more than one set of parenthesis ()
on a single line, should always be read from the innermost out.
While the style of programming above is often used for its
compactness it is disliked by teachers and testers because it can be
done in a clearer manner. Taking the fragment above, if I was using
it for an introductory course I would re-write it as:
def OnOpen(self,e):
""" Handle the file/open menu event."""
self.dirname = '.'
default to the current directory
# Create a file dialogue
dlg = wx.FileDialog(self, "Choose a file",
self.dirname,
“”, “.”, wx.OPEN)
# Show it to the user and get the button pressed
button_pressed = dlg.ShowModal()
if button_pressed == wx.ID_OK:
Did the user press OK?
# Yes
self.filename = dlg.GetFilename()
Get the file name
self.dirname = dlg.GetDirectory() # and the file
location
file_path = os.path.join(self.dirname,
self.filename) # Join them for the file path
file_handle = open(file_path, 'r')
Open the selected file in read mode
content = file_handle.read() # Read the content
self.control.SetValue(content)
Show it to the user
file_handle.close()
Close the file
dlg.Destroy()
Get rid of the dialogue
else: # User pressed cancel
print 'User Cancelled file open'
self.control.SetValue('Nothing Loaded')
This uses a few more lines, and a few temporary variables explicitly
rather than implicitly but is, I think, a lot clearer.
Gadget/Steve
···
On 29/11/2011 5:41 AM, Chris Kavanagh wrote: