Search code examples
pythoneventsdialogwxpythonwxwidgets

WxPython frame not catching EVT_SET_FOCUS


I've set up a frame here and I need to know the active dialog in the main gui so here's what I've come up with:

class PlotFrame ( wx.Frame ):

    def __init__( self, parent, title , dialog_id):
        wx.Frame.__init__(self, None, size=(500, 500), title=title)
        self.parent = parent
        self.dialog_id = dialog_id
        self.Bind(wx.EVT_SET_FOCUS, self.on_focus)

    def __del__( self ):
        pass


    def on_focus(self, event):
        print "Focus:", self.dialog_id
        self.parent.set_active_dialog(self.dialog_id)

This is exactly what the documentation says to do, but my event handler is not called when clicking on the dialog. Running in Linux. help appreciated, thanks


Solution

  • You normally don't set the focus on the frame or the panel. Instead, you would set it to the first child widget in your user interface. In fact, wxPython will usually do this for you automatically such that if you have a button or a text box as your first control, it would typically get the focus by default.

    I don't really see the point in setting the focus to the frame. If all you want to know is if the frame is active, then you should bind to wx.EVT_ACTIVATE instead.