[wxPython] processing EVT_KILL_FOCUS

i derived my custom control class from wxTextCtrl and still have problem
with overriding
EVT_KILL_FOCUS.
One question: how to catch this event also on dialog level ?
MSW, wxpython 2.3.2

marcin andrzejewski
m.andrzejewski@dmz.com.pl
keson@post.pl

from wxPython.wx import *

class myCtrl(wxWindow):
def __init__(self,parent,pos=None,desc="",desc_pos=1, txt="", hor =
wxDefaultSize,ver=-1,style=1):
  self.parent=parent
  self.ver = ver
  self.hor = hor
  self.desc = desc
  self.style=style
  self.Id = NewId()
  txtSize = wxSize(self.hor,self.ver)
  if (pos==None):
   self.pos=wxDefaultPosition
  else:
   self.pos=pos
  self.field = wxTextCtrl(self.parent, self.Id,
txt,self.pos,txtSize,style=self.style,validator=wxDefaultValidator)
  lc = wxLayoutConstraints()
  self.desc = wxStaticText(self.parent, -1, label=self.desc, pos=wxPoint(1,
1),style=wxALIGN_RIGHT|wxST_NO_AUTORESIZE )
  lc = wxLayoutConstraints()
  if (desc_pos ==1):
   lc.right.LeftOf (self.field, 5)
   lc.centreY.SameAs (self.field, wxCentreY)
  else:
   lc.left.SameAs (self.field, wxLeft)
   lc.bottom.SameAs (self.field, wxTop)
  lc.height.AsIs()
  lc.width.AsIs()
  self.desc.SetConstraints(lc)
  EVT_KILL_FOCUS(self.field, self.OnKillFocus)
def OnKillFocus(self,event):
  print 'ctrl'
  event.Skip()

class MyApp(wxApp):
def OnInit(self):
  self.dialog = wxDialog(None, -1, "Test", wxDefaultPosition, wxSize(250,
100))
  self.dialog.ctrlA=myCtrl(self.dialog,(80,20),'User :',hor=140)
  self.dialog.ctrlA.field.SetFocus()
  self.dialog.ctrlB=myCtrl(self.dialog,(80,40),'Password
:',hor=140,style=wxTE_PASSWORD)
  self.dialog.Layout()
  self.dialog.Show(true)
  EVT_CLOSE(self.dialog, self.Close)
  EVT_KILL_FOCUS(self.dialog.ctrlA.field, self.OnKillFocus)
  EVT_KILL_FOCUS(self.dialog.ctrlB.field, self.OnKillFocus)
  return 1
def OnKillFocus(self,event):
  print 'dlg'
  event.Skip()
def Close(self, event):
  self.dialog.Destroy()

app = MyApp(0)
app.MainLoop()

i derived my custom control class from wxTextCtrl and still have problem
with overriding
EVT_KILL_FOCUS.
One question: how to catch this event also on dialog level ?

Focus events are not command events, and so they are not sent to the parent
windows. As you've tried to do in your sample the dialog would have to hook
it's event handler directly to the control. Your problem is that you can't
get both of them to work, right?

It looks like this is because the dynamic event table (which is what
wxPython uses) for any one object is only searched until the first matching
event is found and then searching stops. (But only at that level. If the
parent class has an event table then it is still searched if event.Skip was
called, and if a command event then it gets sent to the parent object as
well.)

THe way to get around this is to push another event handler onto the text
ctrls, then both it and the control's event tables will be searched. Just
derive a class from wxEvtHandler that has a handler method and hook it up
with EVT_KILL_FOCUS just like usual, and then the dialog can create and
instance of this class and call PushEventHandler to push it onto the text
fields.

···

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