[Tutor] Questions about wxEvents

Tiago Saboga tiagosaboga at terra.com.br
Thu Dec 6 18:45:39 CET 2007


On Thu, Dec 06, 2007 at 09:12:59AM -0800, Marc Tompkins wrote:
> I have a specific question - how can I generalize a
> FileBrowseButtonWithHistory - and I realized, as I was trying to word my
> question, that my real question is a bit more generic.
> 
> First, the specific question:  The FileBrowseButtonWithHistory requires a
> callback override and some custom code to straighten out handling the
> history.  So far, so good.  However, I wish to use more than one FBBWH on my
> form, and I can't figure out how to re-use the custom callback so it will
> work for both controls.  (It makes me sick to my stomach when I look at my
> code and see duplicate blocks!)   Don't get me wrong - it's working right
> now, it's just that my code is fugly and I want to clean it up.
> 
> In more general terms, how can I set more than one control to use the same
> block of code as a custom callback, and figure out at runtime which control
> I'm responding to?  Doesn't the Event or CommandEvent carry any information
> about itself?
> I've tried this:
> 
>         [snip]
>         self.fp1 = filebrowse.FileBrowseButtonWithHistory(pnl, -1,
> size=(300, -1),
>             labelText='', fileMask='*.*', fileMode=wx.OPEN,
>             dialogTitle='Select the file containing UCF claims',
> changeCallback=self.fp1Callback)
>         self.fp2 = filebrowse.FileBrowseButtonWithHistory(pnl, -1,
> size=(300, -1),
>             labelText='', fileMask='FI*.*', fileMode=wx.OPEN,
>             dialogTitle='Select the form-image file - generally starts with
> FI', changeCallback=self.fp2Callback)
>         [snip]
>     def fp1Callback(self, evt):
>         print evt.__dict__
>         print help(evt)
>         value = evt.GetString()
>         [snip]
>     def fp2Callback(self, evt):
>         print evt.__dict__
>         print help(evt)
>         value = evt.GetString()
>         [snip]

I have never used wx, but I faced the same question with the qt
toolkit, and I solved it with a function that returns a function. In
my case, I have several widgets to connect to a single function
(setValue), and in some cases they have to be connected also with a
particular function (always named set_AttributeName).

Here's my snippet:

self.connect(widget, QtCore.SIGNAL(signal),
             self.get_setValue_func(option))
[...]
def get_setValue_func(self, option):
    def setIt(value):
       try:
           method = self.__getattribute__("set_%s" % option)
           method(value)
       except AttributeError:
           pass
    self.setValue(option, value)
return setIt
	
Tiago.


More information about the Tutor mailing list