[wxPython-users] parameter passing Question
Christopher Barker
Chris.Barker at noaa.gov
Fri Aug 4 13:02:47 PDT 2006
Daniel Johnson wrote:
> ***************CODE BEGINS************************************
> class MyFrame1(wx.Frame):
> def __init__(self, *args, **kwds):
> .....
> .....
> def OnEdit(self, event):
> sim_name = 'some_string'
> win = MyFrame2(self, -1, "")
> win.Show(True)
sim_name only exists at this point in the local namespace of the OnEdit
method -- you can't get at it from anywhere else at all.
If you put it in self.sim_name, then it will live in the namespace of
the instance of MyFrame1.
> class MyFrame2(wx.Frame):
> def __init__(self, *args, **kwds):
> .....
> .....
> def onLaunch(self, event):
> ## This function needs to use the parameter sim_name which is in
> class MyFrame1-->OnEdit
> ***************CODE ENDS************************************
>
> I want to pass sim_name parameter to MyFrame2 but I don't know how to do
> it.
Where is your MyFrame2 getting created? Unless it's created in
MyFrame1.OnEdit, which would be weird, you can't really just pass it in.
In general, it looks like you need to have some kind of class that holds
various information your app is working with. I'm guess that sim_name
isn't the only piece of data that you'll need to do this kind of thing with.
Then you need to be able to reference that data object from both Frame1
and Frame2, and probably a lot of other places. There are a lot of way
to do that. One of which is....
> I tried couple of things but always got some error. I have some parameter
> passing in normal Python code but *args and **kwds is a little confusing.
You could do something like:
class MyFrame2(wx.Frame):
def __init__(self, MyData, *args, **kwds):
then you would create it like:
AFrame = MyFrame2(SomeData, parent, other, parameters, to, wx.Frame)
If wxGlade Doesn't like that, you can just set the MyData parameter
after you create the object;
AFrame = MyFrame2(parent, other, parameters, to, wx.Frame)
AFrame.MyData = SomeData
You could also store this data in the wx.App object, in a module that
you reference from anywhere you need it, etc, etc, etc.
Remember: Python is ALL about namespaces. Every Python object (a class,
a module, an instance, etc, is really just a namespace)
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
More information about the wxpython-users
mailing list