Still experimenting with using wxpython. I have made reasonable progress ,
but I know my issues
have to do with OOP and class(s).
In the attached code snippet , the form and panel display as expected. When
attempting to execute
a button2 event, it does execute with the following exception :
Traceback (most recent call last):
File "multiLineMod.py", line 34, in B2_click
self.tbox2.SetValue("clicked") #<====== error here, doesn't
execute
AttributeError: 'MyFrame' object has no attribute 'tbox2'
I have tried combinations on the line that errors including changing self to
panel and also
just eliminating any preface (panel or self).
I can not understand how tbox2 seems to display and is added in the sizer in
the script but then is not recognized in the the event code and I am
generating the error.
I did place a print statement inside the event to verify the event did
trigger and that verifies. multiLineMod.py
<http://wxpython-users.1045709.n5.nabble.com/file/n5721004/multiLineMod.py>
···
--
View this message in context: http://wxpython-users.1045709.n5.nabble.com/attribute-errors-I-don-t-get-it-tp5721004.html
Sent from the wxPython-users mailing list archive at Nabble.com.
You are creating tbox2 as a local variable. That variable is destroyed at the end of the init method. If you want to be able to use it in your event handler, then you need to declare tbox2 as self.tbox2 in your init.
···
On Monday, May 5, 2014 4:30:03 PM UTC-5, EightBits wrote:
Still experimenting with using wxpython. I have made reasonable progress ,
but I know my issues
have to do with OOP and class(s).
In the attached code snippet , the form and panel display as expected. When
attempting to execute
a button2 event, it does execute with the following exception :
Traceback (most recent call last):
File “multiLineMod.py”, line 34, in B2_click
self.tbox2.SetValue(“clicked”) #<====== error here, doesn’t
execute
AttributeError: ‘MyFrame’ object has no attribute ‘tbox2’
I have tried combinations on the line that errors including changing self to
panel and also
just eliminating any preface (panel or self).
I can not understand how tbox2 seems to display and is added in the sizer in
the script but then is not recognized in the the event code and I am
generating the error.
I did place a print statement inside the event to verify the event did
trigger and that verifies. multiLineMod.py
<http://wxpython-users.1045709.n5.nabble.com/file/n5721004/multiLineMod.py>
eightbits wrote:
I have tried combinations on the line that errors including changing self to
panel and also
just eliminating any preface (panel or self).
I can not understand how tbox2 seems to display and is added in the sizer in
the script but then is not recognized in the the event code and I am
generating the error.
This is an issue of variable scope. All of the code to set up the
layout of your frame/panel and bind your events takes place inside the
__init__ method of your MyFrame class. So, when you reference panel,
tbox2, etc., everything works as expected. Python knows what panel and
tbox2 are because you've just defined them in the same method.
However, because you have not made them instance variables by prepending
self to their names, as soon as any code outside of the __init__ method
tries to access self.panel, self.tbox2, whatever, you get an
AttributeError because there is no such instance variable. How does
Python know that when you say self.tbox2, you want the tbox2 variable
which was defined in the __init__ method with only local scope? It
doesn't, and thus, it doesn't work.
In short, anywhere you're assigning or referencing your text boxes in
your code, or any variable for that matter which will be referenced
outside of __init__, you should prepend self to their names. So:
self.tbox1=wx.TextCtrl(panel, -1,"TxtBox1")
self.tbox2=wx.TextCtrl(panel,-1,"")
...
sizer.Add(self.tbox1)
sizer.Add(self.tbox2)
...
self.tbox2.SetValue("clicked")
···
--
James Scholes
http://twitter.com/JamesScholes
Thanks Mike. I also got an in depth explanation and thanks to the both of
you, it worked!
···
--
View this message in context: http://wxpython-users.1045709.n5.nabble.com/attribute-errors-I-don-t-get-it-tp5721004p5721007.html
Sent from the wxPython-users mailing list archive at Nabble.com.
Thanks a million James. I made the changes and it executed as expected.
One step forward, hopefully less steps back
···
--
View this message in context: http://wxpython-users.1045709.n5.nabble.com/attribute-errors-I-don-t-get-it-tp5721004p5721008.html
Sent from the wxPython-users mailing list archive at Nabble.com.