[wxPython-users] widget suggestion / question

Stuart McGraw smcg4191 at frii.com
Sun Dec 2 17:42:21 PST 2007


Mark Erbaugh wrote:
> Steve,
> 
> Thanks for the reply.
> 
> On Sun, 2007-12-02 at 10:08 -0800, Stephen Hansen wrote: 
>>         In Borland Delphi, the individual widgets (components) had a
>>         spare field
>>         that could be used to store an integer for whatever purpose
>>         the 
>>         developer wanted.  I would like to see something like this in
>>         wx. Here's
>>         where I could use something like this. I designed a form with
>>         a series
>>         of buttons. Several of the buttons could share the same event
>>         handler if 
>>         the event handler could figure out which button invoked it.  I
>>         supposed
>>         I could use reflection to figure out which button, but it
>>         would be nice
>>         if the event handler could check this spare field in the
>>         button. 
>>
>>
>> Since every button has it's own ID, isn't .GetID() enough to find
>> which is which? 
> 
> That certainly is an approach. But, would that mean that I would have to
> assign a known ID to the button rather than using -1?  One problem I see
> with this is that I would have to modify my handler if I modified the
> GUI. Perhaps I added a new button that I wanted to be handled
> identically to an existing button.  That button would have to have a
> different ID so I would have to modify the handler to look for the new
> ID.
> 
>>
>>         I'm using wxGlade for the design. The latest version allows
>>         you to
>>         attach properties to the widgets which it implements by
>>         calling a SetXXX
>>         method. To get this to work, I had to derive a button from
>>         wxButton that 
>>         had a SetXXX method. 
>>
>> As I find it more efficient to do all my GUI design by hand, I don't
>> know what wxGlade's capabilities are. Does it let you enter arbitrary
>> code? As all the wxPython objects are regular python objects, you can
>> set any arbitrary attribute on them if you want. I personally find
>> that "poor practice", but that's me.
> 
> Sometimes I do my own hand-coding and I have set arbitrary attributes.
> Like you, I somehow feel that this is not right, but it's perfectly good
> Python. That's why I'd kind of like an 'official' wxPython attribute.
> Since Python is dynamically typed this attribute could hold whatever tne
> programmer needed.
> 
>>
>> You could do 'button.whichButton = 5' if you wanted. Even though
>> 'whichButton' doesn't exist before. 
> 
> The new version of wxGlade does support arbitrary code. I haven't tried
> that feature, but it might work for what I need. Thanks for the
> suggestion.

Not sure if this is useful with Glade or not but if you don't like
setting attributes on objects you don't own, what I have done in the
past is use lambda or functools.partial to bind an extra argument
to the handler call.  I use strings below but obviously one could
substitute int's or arbitrary objects.
[Warning, untested code from biological memory!]

     ...in frame ...
         button1.Bind (wx.EVT_BUTTON, lambda x: self.button_hndlr ("button 1", x))
         button2.Bind (wx.EVT_BUTTON, lambda x: self.button_hndlr ("button 2", x))
     ...
     def button_hndlr (self, button_data, evt=None):
	if button_data == "button 1":
	    ...
         elif button_data == "button 2":
	    ...

I'm ok with lambda but there are those who aren't, in which case
functools.partial can be used for simpler syntax at the cost of
an import:

   from functools import partial
      ...
         button1.Bind (wx.EVT_BUTTON, partial(self.button_hndlr, "button 1"))

HTH.









More information about the wxpython-users mailing list