Using an object inside a class
ian.g.kelly at gmail.com
Mon Jan 23 17:20:55 EST 2012
On Mon, Jan 23, 2012 at 2:52 PM, Jonno <jonnojohnson at gmail.com> wrote:
> On Mon, Jan 23, 2012 at 3:42 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>> Exactly. The line "app = MyApp(0)" creates a MyApp instance and then
>> assigns it to "app". As part of the MyApp creation process, it
>> creates a MyFrame, which creates a Tab, which creates a Class1, which
>> attempts to reference "app". All of this happens before that
>> "MyApp(0)" call has returned, so the result of that call has not
>> actually been assigned to "app" yet.
>> I suggest using wx.GetApp() instead.
> That totally makes sense. However I'm not sure I understand your suggestion
> how to use wx.GetApp()
> Isn't the wxApp still not created before Class1 is instantiated so I still
> can't call wx.GetApp() in __init__ of Class1 can I?
The App object is created and the wx framework already knows about it.
It's just not assigned to the app global yet, and the OnInit call has
not completed yet. See:
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
>>> class MyApp(wx.App):
... def OnInit(self):
... print "wx.GetApp() =", wx.GetApp()
... print "app =", app
... return True
>>> app = MyApp(0)
wx.GetApp() = <__main__.MyApp; proxy of <Swig Object of type 'wxPyApp
*' at 0x18d8fc0> >
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
line 7823, in __init__
line 7420, in _BootstrapApp
return _core_.PyApp__BootstrapApp(*args, **kwargs)
File "<stdin>", line 4, in OnInit
NameError: global name 'app' is not defined
More information about the Python-list