I've got a combo box connected to a database. If the user selects an item in the list, fine; if not, then when the combo box loses focus, it triggers a new dialog offering the user the chance to add that item.
The first time through, this works fine. Subsequently, however, it gets the program stuck in an infinite loop. Every time I exit the new dialog, it immediately triggers the KillFocus event for the combo box, for no apparent reason. In fact, if I tab to the combo box rather than using the mouse to get to it, the KillFocus event (and hence the dialog) is triggered before I actually get into the combo box. It is as though the SetFocus event were triggering the dialog instead of the KillFocus event.
This is on WindowsXP, Python 2.3, wxPython 2.4. The KillFocus event looks like this:
def OnCbocompanyKillFocus(self, event):
if self.cboCompany.FindString(self.cboCompany.GetValue())==-1:
x=AddCompany.AddCompany(self)
x.ShowModal()
event.Skip()
The dialog closes itself through one of the following functions:
def checkEnd(self,event):
if event.GetKeyCode() == 27:
self.EndModal(0)
I've got a combo box connected to a database. If the user selects an item in the list, fine; if not, then when the combo box loses focus, it triggers a new dialog offering the user the chance to add that item.
The first time through, this works fine. Subsequently, however, it gets the program stuck in an infinite loop. Every time I exit the new dialog, it immediately triggers the KillFocus event for the combo box, for no apparent reason.
Focus events are tricky. Probably what is happening in this case is that after the dialog is done it is trying to restore the focus to the window that had it before the dialog was shown, which is the combobox again. Then your OnCbocompanyKillFocus returns and the default handler then moves the focus to the next control, thereby causing the combobox to lose the focus again...
Try doing the dialog later with wxCallAfter or something.
···
--
Robin Dunn
Software Craftsman http://wxPython.org Java give you jitters? Relax with wxPython!
I've got a combo box connected to a database. If the user selects an item in the list, fine; if not, then when the combo box loses focus, it triggers a new dialog offering the user the chance to add that item.
The first time through, this works fine. Subsequently, however, it gets the program stuck in an infinite loop. Every time I exit the new dialog, it immediately triggers the KillFocus event for the combo box, for no apparent reason.
Focus events are tricky. Probably what is happening in this case is that after the dialog is done it is trying to restore the focus to the window that had it before the dialog was shown, which is the combobox again. Then your OnCbocompanyKillFocus returns and the default handler then moves the focus to the next control, thereby causing the combobox to lose the focus again...
Try doing the dialog later with wxCallAfter or something.
Thanks for your help. Sometimes I think that the inventor of event-driven programming was trying to drive me insane -- sometimes events get fired in rapid succession and no intuitive order, and trying to debug only creates new events that screw up the order further (the Heisenberg Uncertainty Principle of GUI programming?).
Somehow this problem went away, I think partly because I realized I needed to add event.Skip(), but mostly because I no longer allow the user to move to another control until he has picked an item in the list (even if one he adds himself).