收录日期:2019/05/21 01:12:02 时间:2009-07-29 17:58:41 标签:python,user-interface,event-handling,wxpython

I have a wxPython notebook, in this case a wx.aui.AuiNotebook. (but this problem has happened with other kinds of notebooks as well.) In my notebook I have a widget, in this case a subclass of ScrolledPanel, for which I am trying to do some custom event handling (for wx.EVT_KEY_DOWN). However, the events are not being handled. I checked my code outside of the notebook, and the event handling works, but when I put my widget in the notebook, the event handler doesn't seem to get invoked when the event happens.

Does the notebook somehow block the event? How do I solve this?

I tried reproducing your problem but it worked fine for me. The only thing I can think of is that there is one of your classes that also binds to wx.EVT_KEY_DOWN and doesn't call wx.Event.Skip() in its callback. That would prevent further handling of the event. If your scrolled panel happens to be downstream of such an object in the sequence of event handlers it will never see the event.

For reference, here's an example that worked for me (on Windows). Is what you're doing much different than this?

import wx
import wx.aui, wx.lib.scrolledpanel

class AppFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)

        # The notebook
        self.nb = wx.aui.AuiNotebook(self)

        # Create a scrolled panel
        panel = wx.lib.scrolledpanel.ScrolledPanel(self, -1)
        panel.SetupScrolling()
        self.add_panel(panel, 'Scrolled Panel')

        # Create a normal panel
        panel = wx.Panel(self, -1)
        self.add_panel(panel, 'Simple Panel')

        # Set the notebook on the frame
        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.nb, 1, wx.EXPAND)
        self.SetSizer(self.sizer)

        # Status bar to display the key code of what was typed
        self.sb = self.CreateStatusBar()

    def add_panel(self, panel, name):
        panel.Bind(wx.EVT_KEY_DOWN, self.on_key)
        self.nb.AddPage(panel, name)

    def on_key(self, event):
        self.sb.SetStatusText("key: %d [%d]" % (event.GetKeyCode(), event.GetTimestamp()))
        event.Skip()

class TestApp(wx.App):
    def OnInit(self):
        frame = AppFrame(None, -1, 'Click on a panel and hit a key')
        frame.Show()
        self.SetTopWindow(frame)
        return 1

if __name__ == "__main__":
    app = TestApp(0)
    app.MainLoop()