[wxPython-users] Re: Finding which control has focus

Robin Dunn robin at alldunn.com
Fri Oct 5 10:35:36 PDT 2007


Larry Bates wrote:
> Robin Dunn wrote:
>> Larry Bates wrote:
>>> I have a frame that contains 6 controls: ComboBox, ExpandoTextCtrl, 
>>> and 2 list controls, cancel button, send button.  I want to know if 
>>> there is some way to
>>> trap the fact that a user clicked the mouse on the ComboBox, 
>>> ExpandoTextCtrl or
>>> either of the 2 list controls.  I would like to make the label bold 
>>> to show which control has the focus.  onSetFocus/onKillFocus doesn't 
>>> fire when you click on these controls.  I can't even get it to fire 
>>> when I do MyExpandoTextCtrl.SetFocus() (but the focus does change) Is 
>>> there some other way?  I don't have these on separate windows or panels.
>>
>>
>> How are you binding the events to those methods?  (Some sample code is 
>> worth 1000 words...)
>>
>> To answer the question in your Subject, there is the 
>> wx.Window.FindFocus static method.
>>
>>
> Here is a working example of my problem.  I've stripped it down as much 
> as I could.  I just can't seem to get any of the focus events to 
> fire/call my methods.  Thanks for taking a look.
> 

>         self.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
>         self.Bind(wx.EVT_KILL_FOCUS, self.onKillFocus)
>         self.Bind(wx.EVT_SET_FOCUS, self.FCB.onSetFocus, self.FCB)
>         self.Bind(wx.EVT_KILL_FOCUS, self.TTC.onKillFocus, self.FCB)
>         self.Bind(wx.EVT_SET_FOCUS, self.TTC.onSetFocus, self.TTC)
>         self.Bind(wx.EVT_KILL_FOCUS, self.TTC.onKillFocus, self.TTC)
>         self.Bind(wx.EVT_SET_FOCUS, self.LCU.onSetFocus, self.LCU)
>         self.Bind(wx.EVT_KILL_FOCUS, self.LCU.onKillFocus, self.LCU)
>         self.Bind(wx.EVT_SET_FOCUS, self.LCF.onSetFocus, self.LCF)
>         self.Bind(wx.EVT_KILL_FOCUS, self.LCF.onKillFocus, self.LCF)

Change these to look like this instead:

	self.FCB.Bind(wx.EVT_SET_FOCUS, self.FCB.onSetFocus)

(or move them into the other classes to better encapsulate the 
functionality.) You can read 
http://wiki.wxpython.org/self.Bind_vs._self.button.Bind for an 
explanation of the difference.  (Hint: focus events are not command events.)


-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!






More information about the wxpython-users mailing list