Amend the line in init as follows:
self.grid = wx.grid.Grid(etc…)
and then the grid will be visible all over the class as self.grid.
Or assign a specific name to the grid in the constructor, as follows:
grid = wx.grid.Grid(etc…, name = ‘The Grid’)
and then get back the grid wherever you need it with the function:
self.FindWindowByName(‘TheGrid’).
This second solution lessens the overhead on memory, so it is to be preferred for all controls that are called only now and then, which is not normally the case with grids :-).
Or assign a specific name to the grid in the constructor, as follows:
grid = wx.grid.Grid(etc..., name = 'The Grid')
and then get back the grid wherever you need it with the function:
self.FindWindowByName('TheGrid').
This second solution lessens the overhead on memory,
by one reference -- which is tiny. And it adds the name reference to the grid (or there always one there?), so it's negligible.
> so it is to be
preferred for all controls that are called only now and then,
I don't think it's preferred at all. I never use it, though if I were to, it would be because I needed to reference the Window from somewhere far away in the object hierarchy, making keeping a direct reference hard (though that sound like a design in need of refactoring!)
-Chris
···
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
I worry more about performance than memory. It seems to me
that
self.FindWindowByname(name)
must execute way slower than
self.grid
You are correct. The latter is a very fast dictionary access. The former marshals the parameters onto the stack, makes the function call to the wrapper function, converts the string to a unicode value, deals with the GIL, calls the wx C++ method which does a recursive descent search of the window and it's children doing a unicode string compare of the name of each window until it encounters a match, then returns to the wrapper function where it will check if there is already a Python proxy object for that C++ object, and construct one if there isn't one, and then deal with the GIL again and then finally return the reference to the proxy object. Doesn't seem worth it just to save 4-8 bytes and an entry in the self object's dictionary.
···
--
Robin Dunn
Software Craftsman http://wxPython.org Java give you jitters? Relax with wxPython!
I tried to reflect on what Chris wrote: being this one a matter of economy, it should be possible to calculate it, more or less accurately.
Every window in a class has a name, even if sometimes it is the default name, and it has to be stored somewhere in memory. So to assign a specific name to a control doesn't unduly impinge on memory
One thing that isn't being thought of here is that when you use something like FindWindowByName("some name") then not only is there the string stored in the window, but there is also the string constant in the code used to make the call to FindWindowByName.
On the other hand, wxPython is already huge so a few more bytes for either approach doesn't matter much at all when it is just 0.01% (or less) of the whole thing. So just use whatever is easiest for you and don't worry about memory consumption at that scale.
···
--
Robin Dunn
Software Craftsman http://wxPython.org Java give you jitters? Relax with wxPython!
I tried to reflect on what Chris wrote: being this one a matter of economy, it should be possible to calculate it, more or less accurately.
Every window in a class has a name, even if sometimes it is the default name, and it has to be stored somewhere in memory. So to assign a specific name to a control doesn’t unduly impinge on memory
One thing that isn’t being thought of here is that when you use something like FindWindowByName(“some name”) then not only is there the string stored in the window, but there is also the string constant in the code used to make the call to FindWindowByName.
On the other hand, wxPython is already huge so a few more bytes for either approach doesn’t matter much at all when it is just 0.01% (or less) of the whole thing. So just use whatever is easiest for you and don’t worry about memory consumption at that scale.