wx.EVT_KILL_FOCUS not called?

I'm trying to get a dialog to pop up when a user clicks a button. The dialog should appear over the top of the button and present them with a means to change a value (via sliders, text boxes etc.). When the user then clicks elsewhere on the interface I'd like the dialog to disappear. The thing is, I never seem to get any wx.EVT_KILL_FOCUS messages so it's impossible to tell when the user has clicked elsewhere!

I'd be very grateful if anyone has any ideas what I'm doing wrong? An simplified example of my code is listed below:

···

----

import wx

class MyForm( wx.Dialog ):
    def __init__(self, parent, *args, **kwargs):
        if( kwargs.has_key( 'timeValue' ) ):
            self.value = kwargs['timeValue']
            del kwargs['timeValue']
        else:
            self.value = 0
        wx.Dialog.__init__(self, parent, *args, **kwargs)

        backgroundColour = wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW )
        backgroundColour.Set(backgroundColour.Red()/2, backgroundColour.Red()/2, backgroundColour.Red()/2)
        self.SetBackgroundColour(backgroundColour)
        self.slider = wx.Slider(self, -1, self.value)
        self.Bind(wx.EVT_KILL_FOCUS, self.OnLosingFocus)

    def OnLosingFocus(self, event):
        newWindowInFocus = event.GetWindow()
        dialogShouldClose = True
        for child in self.GetChildren():
            if (child == newWindowInFocus):
                dialogShouldClose = False
        if (dialogShouldClose):
            self.Close()
    class MyButton( wx.Button ):
    def __init__(self, parent, *args, **kwargs):
        wx.Button.__init__(self, parent, *args, **kwargs)
        self.Bind(wx.EVT_BUTTON, self.OnClick)
        self.value = 0

    def OnClick(self, event):
        sizeOfDialog = self.GetSize()
        positionOfDialog = self.GetScreenPosition()
        dlg = MyForm(self, -1, "time window", pos=positionOfDialog, size=sizeOfDialog, style=wx.NO_BORDER, timeValue=self.value )
        dlg.Show(True)

class MyFrame(wx.Frame):
    def __init__(self, parent, id=-1, title='focus test', pos = wx.DefaultPosition,
                 size=(100, 100), style=wx.DEFAULT_FRAME_STYLE):
        wx.Frame.__init__(self, parent, id, title, pos, size, style)
        button = MyButton(self)
        button.Show()

app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()

Jon wrote:

I'm trying to get a dialog to pop up when a user clicks a button. The dialog should appear over the top of the button and present them with a means to change a value (via sliders, text boxes etc.). When the user then clicks elsewhere on the interface I'd like the dialog to disappear. The thing is, I never seem to get any wx.EVT_KILL_FOCUS messages so it's impossible to tell when the user has clicked elsewhere!

I'd be very grateful if anyone has any ideas what I'm doing wrong? An simplified example of my code is listed below:

The dialog itself is not ever going to have the focus if it has children, one of them will have it. You might try using EVT_ACTIVATE instead.

···

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