wxpython parameter to EVT_MENU func

Enrique ecastro at cicei.ulpgc.es
Thu Jan 24 04:45:23 EST 2002


Thomas Schultz wrote:
> 
> Thanx a lot Hans
> 
> I'll try your code, in the meantime I used a workaround with
> 
> def __init__(self, parent, log):
>         global grid # makes the grid accessable for all modules
>           grid = SimpleGrid(self, log)
> 
> but maybe this can invoke memory problems
> 
> Thomas
> 
Try to attach to the advice by Hans Nowak.
Your code above has in my opinion, perhaps I am
extrapolating your mind too much from your code, a
misconception about "global" keyword.

"global" does not "# makes the grid accessable for all
modules". 
It is not a declaration of the variable grid as a global
variable.
The "global" keyword just tells the interpreter to use the,
existing, variable grid that was assigned in an upper scope.
It avoids re-binding the variable name to a local object. 
So, in your code, "grid = SimpleGrid(self, log)" should be
placed outside, and executed before the __init__ function is
called. 


Regarding you first question, in many GUI toolkits, the
phylosophy behind is ala follows:
You have a grid (or whatever) inside a frame. The frame
"owns" the grid. Thus, Frame class holds a reference to the
grid as a data member (self.grid=MyGrid(...)). This is
applicable to any widget inside any container (other
widget).
The data in the grid may be stored in a different data
structure outside the Frame class, and may be shared by
otehr classes/functions. That's a matter of design and
taste.

In wxPython you may pass any additional parameters to the
methods called by EVT_XXX, at you will. But in you code the
reference to grid is probably wrong.


> >>>>>>>>>>>>>>>>>> Ursprüngliche Nachricht <<<<<<<<<<<<<<<<<<
> 
> Am 23.01.2002, 18:07:06, schrieb Hans Nowak <wurmy at earthlink.net> zum Thema
> Re: wxpython parameter to EVT_MENU func:
> 
> > Thomas Schultz wrote:
> > >
> > > Hi,
> > >
> > > how can I pass an object to a function called by EVT_MENU?
> > >
> > > I'm writing an application using wxgrid. I created a menu entry called
> > > SAVE. I call this function with:
> > >
> > > EVT_MENU(self, ID_SAVE, self.Save)
> > >
> > > def Save(self, event):
> > >         ....
> > > In Save I have no acces to the grid, I tried:
> > >
> > > EVT_MENU(self, ID_SAVE, self.Save, grid)
> > >
> > > def Save(self, event, grid):
> > >         ....
> > >
> > > but this doesn't work.
> > >
> > > How can I acces the grid in the function?
> 
> > I usually use something like (pseudo-code!):
> 
> > class MyFrame(wxFrame):
> >     def __init__(self, parent, id):
> >         wxFrame.__init__(...)
> >         ...create menu...
> >         EVT_MENU(self, ID_SAVE, self.Save)
> >         self.grid = MyGrid(...)
> >     def Save(self, event):
> >         # now here you can access the MyGrid instance
> >         # through self.grid.
> 
> > I'm not exactly a wxPython expert, so maybe there are
> > better ways to do it. But this works for me.
> 
> > --
> > Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
> > # decode for email address ;-)
> > The Pythonic Quarter:: http://www.awaretek.com/nowak/



More information about the Python-list mailing list