Finding which control has focus

Larry Bates larry.bates at websafe.com
Wed Oct 10 05:41:17 PDT 2007


Robin Dunn wrote:
> 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.)
> 
> 
Thanks for the tip.  I'll review the style guide in some detail.  This is my 
first REAL wx project, so I'm learning a lot.  I have to admit that much of the 
"structure" was done for me by wxGlade.  Perhaps it doesn't do the best job but 
it sure helped a LOT.

-Larry





More information about the wxpython-users mailing list