I hope this question isn't beyond the scope of this group\. If it is,
I apologize. I'm new not only to wxPython but Python & Programming in
general. I began learning Programming/Python roughly 3 months ago.
I understand basically what's going on with the code below, however I'm
a little confused about the lines of code after the FileDialog
constructor. Lines (6-10) are where my questions lay. . .I understand
that line 4 opens the file open window. Line 5's if statement I
understand. Line 6,7,8,9 I don't really get, and was hoping someone
could explain to me what each of these lines is doing??? Thank You for
ANY help!
GetFilename() returns the current file name. .
GetDirectory() returns the current working directory. .
But how are we using these methods in the code? Why are they there, and
what exactly are they doing??
Then line 8 I don't get at all. .
And line 9, I'm completely lost. .
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()
self.filename and self.dirname are attributes of self. self is likely
your frame
"self" is an variable name that points to an "instance" of some class;
meaning you've created some object and you are reffering to that
object with the varible name "self". "self" is a variable name
pointing to that instance object.
The line "def OnOpen(self,e)" defines a method. That method, OnOpen(),
is part of a class. "def OnOpen"; The "def" part means "I'm going to
DEFine a method. defs are templates for creating method objects. And
classes are templates for creating instance objects.
Unlike most other languages, Python (and hence wxPython) expects the
first argument of a method "def"inition to be a "instance object" of
the class the method is apart of. Other languages, self/this/me are
implied and usually are not required by the programmer to be
specified. Python (practically) requires it.
There are many many posts on this point, said many different ways. I
tried to use the common terms. But understanding these terms and how
classes and methods are syntactically structured will allow you to
understand lines of code like "self.filename = dlg.GetFilename()"
If you define a class definition like:
class Person(object): # creates a class definition or a "class" named
"Person"
language = 'gibberish' # a "class" attribute named "language"
that point to a string object 'gibberish'
def __init__(self, name, age, lost): # creates a method named
"__init__"
self.name = name
self.age = age
longlost = lost # not saved; no "self"
print name, ' gets lost', longlost
def show(self): # creates a method named "show" of class Person
print "Name:", self.name # self.name is an instance
attribute
print "Age:", self.age # self.age; same also called just
"attribute"
now you can create a whole bunch of instances of "Person"
dp = Person("DevPlayer", 42, 'inspace') # creates an instance
myalias = dp # creates another varible
name; not another Person object
ck = Person("Chris", 111, 'inpython') # creates an instance
rd = Person("Robin", 2011, 'inpheonix') # creates an instance
But how do you show the attributes of each "instance".
# same question different vocabulary
But how do you show values of the attributes of each "instance".
Try:
print dp.name <<< something I typed in the python interperter
DevPlayer
print ck.age
111
rd.show()
Robin
2011
myalias.show()
DevPlayer
42
"myalias" is just another variable name that points to the same
"object" that the varible name "dp" points too.
In Python variable names (aka variables) are like pointers. Variables
do not "HOLD" data. That is an important point. Variables are "names"
that point to data. "self" is a variable name that points to an
object. That object has attributes. An attribute is a name that points
to an object. So "self.filename" is a name, "self", pointing to an
object, probably an instance of wx.Frame() maybe named "frame". And
the frame object needs to save the filename, perhaps something like
"autoexec.bat", or whatever, into an attribute, named "filename" which
is a name.
But how does Person.show() know how to print out dp.name or ck.age?
in Person def show(self), "self" points to the instance object for
"dp", "ck" or "rd". Not the class definition of Person. In
Person.__init__() the variable names "name" and "age" are assigned to
be attributes of each instance dp, ck and rd when they are created.
So self.name could be dp.name or ck.name or rd.name.
first: methods are like functions.
"dlg" is a varable name that points to a "dialog box instance object"
and "GetFilename" is a method name defined in wx.DialogBox and added
to the dialog box instance. You've reffered to your dialog box
instance with the variable name "dlg".
The line "self.filename = dlg.GetFilename()"
means assign the value, probably a string object like
"autoexec.bat",
into the attribute named "filename"
of the current object instance, named "self",
which is probably a frame or window,
which is returned from the method named "GetFileName",
which is a method in the object, a dialogbox,
referred to by the variable name "dlg",
···
On Nov 25, 11:26 pm, Chris Kavanagh <cka...@gmail.com> wrote: