wx.Frame and KeyboardFocus

Hello guys,

Today I'm busy writing a simple wxPython application, but I have some
problems with it. As soon as I want to attach a Keyboard listener,
wx.Bind() returns as expected, but events won't be registered.

Below is a minimal test case of the situation I expect to work. Do you
know why the KeyBoardEvent() function isn't triggered? The frame even
explicitly asks for focus, but the function is never called.
self.AcceptsFocusFromKeyboard() also returns false.

#!/usr/bin/python
import wx

class Frame(wx.Frame):
    def __init__(self, title):
        wx.Frame.__init__(self, None, title=title, size=(350,200))
        self.SetFocus()
        self.Bind(wx.EVT_KEY_DOWN, self.KeyBoardEvent)
        print "Focus?: " + str(self.AcceptsFocusFromKeyboard())

    def KeyBoardEvent(self, event):
        print event

if __name__ == '__main__':
    app = wx.App()
    fr = Frame("wx.EVT_KEY_DOWN")
    fr.Show()
    app.MainLoop()

Hello guys,

Today I'm busy writing a simple wxPython application, but I have some
problems with it. As soon as I want to attach a Keyboard listener,
wx.Bind() returns as expected, but events won't be registered.

On some platforms the frames can not deal with key events, try putting a panel or some other window type in the frame and bind the event handler there.

Below is a minimal test case of the situation I expect to work. Do you
know why the KeyBoardEvent() function isn't triggered? The frame even
explicitly asks for focus, but the function is never called.
self.AcceptsFocusFromKeyboard() also returns false.

Checking HasFocus would be a better test there.

import wx
print wx.version()

class Frame(wx.Frame):
     def __init__(self, title):
         wx.Frame.__init__(self, None, title=title, size=(350,200))
         pnl = wx.Panel(self)
         pnl.SetFocus()
         pnl.Bind(wx.EVT_KEY_DOWN, self.KeyBoardEvent)
         print "Focus?: " + str(pnl.HasFocus())

     def KeyBoardEvent(self, event):
         print event

if __name__ == '__main__':
     app = wx.App()
     fr = Frame("wx.EVT_KEY_DOWN")
     fr.Show()
     app.MainLoop()

···

On 2/15/12 11:57 PM, Kevin van der Vlist wrote:

--
Robin Dunn
Software Craftsman

Hello guys,

Today I'm busy writing a simple wxPython application, but I have some
problems with it. As soon as I want to attach a Keyboard listener,
wx.Bind() returns as expected, but events won't be registered.

On some platforms the frames can not deal with key events, try putting a
panel or some other window type in the frame and bind the event handler
there.

Yes, this is the problem. Adding a panel with a size of (0,0) allows me to receive the keyboard events. Thanks for the tip. Perhaps it would be nice to place a note about this on wxPython API Documentation — wxPython Phoenix 4.2.2 documentation or similar? Something like the current win32 notice.

For future reference: this is on Linux, using KDE4.

Below is a minimal test case of the situation I expect to work. Do you
know why the KeyBoardEvent() function isn't triggered? The frame even
explicitly asks for focus, but the function is never called.
self.AcceptsFocusFromKeyboard() also returns false.

Checking HasFocus would be a better test there.

HasFocus() was false as well, I tested it before I sent the mail.

···

On 16/02/12 18:16, Robin Dunn wrote:

On 2/15/12 11:57 PM, Kevin van der Vlist wrote:

import wx
print wx.version()

class Frame(wx.Frame):
def __init__(self, title):
wx.Frame.__init__(self, None, title=title, size=(350,200))
pnl = wx.Panel(self)
pnl.SetFocus()
pnl.Bind(wx.EVT_KEY_DOWN, self.KeyBoardEvent)
print "Focus?: " + str(pnl.HasFocus())

def KeyBoardEvent(self, event):
print event

if __name__ == '__main__':
app = wx.App()
fr = Frame("wx.EVT_KEY_DOWN")
fr.Show()
app.MainLoop()

Kevin van der Vlist wrote:

Today I'm busy writing a simple wxPython application, but I have some
problems with it. As soon as I want to attach a Keyboard listener,
wx.Bind() returns as expected, but events won't be registered.

Below is a minimal test case of the situation I expect to work. Do you
know why the KeyBoardEvent() function isn't triggered? The frame even
explicitly asks for focus, but the function is never called.
self.AcceptsFocusFromKeyboard() also returns false.

Your code works perfectly for me. What version and operating system are
you running?

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Yeah, Robin already said it wouldn't work on *some* systems, so that would be the culprit. His solution of adding a frame worked. The problem is solved :).

As I said in the previous email, I'm running on Linux, using KDE as DE.

I'm using the following wx version:
kevin@iussaaset:~$ dpkg -l | grep python-wxgtk
ii python-wxgtk2.8 2.8.12.1-7 wxWidgets Cross-platform C++ GUI toolkit (wxPython binding)

Regards,

Kevin van der Vlist

···

On 16/02/12 22:16, Tim Roberts wrote:

Kevin van der Vlist wrote:

Today I'm busy writing a simple wxPython application, but I have some
problems with it. As soon as I want to attach a Keyboard listener,
wx.Bind() returns as expected, but events won't be registered.

Below is a minimal test case of the situation I expect to work. Do you
know why the KeyBoardEvent() function isn't triggered? The frame even
explicitly asks for focus, but the function is never called.
self.AcceptsFocusFromKeyboard() also returns false.

Your code works perfectly for me. What version and operating system are
you running?

Kevin van der Vlist wrote:

Yeah, Robin already said it wouldn't work on *some* systems, so that
would be the culprit. His solution of adding a frame worked. The problem
is solved :).

Yes, that's the problem with a short memory. I made a quick scan
through my email this morning, and hit "reply" on yours so I would
remember to reply to it. I then read Robin's reply, and realized mine
was superfluous.

Later in the day, when closing windows, that uncompleted reply popped
back up. Having forgotten everything from this morning, I finished the
reply and sent it...

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Yeah, sounds familiar. But let's look at the bright side, I'm glade you took the time to reply to the message. If I hadn't received the answer already, I'd have welcomed it. So thanks anyway.

I still think this is something that's worth of a note in the documentation though. It is strange behavior, and a bit daunting when you roll into wxPython without any prior knowledge about the issue.

···

On 16/02/12 23:07, Tim Roberts wrote:

Kevin van der Vlist wrote:

Yeah, Robin already said it wouldn't work on *some* systems, so that
would be the culprit. His solution of adding a frame worked. The problem
is solved :).

Yes, that's the problem with a short memory. I made a quick scan
through my email this morning, and hit "reply" on yours so I would
remember to reply to it. I then read Robin's reply, and realized mine
was superfluous.

Later in the day, when closing windows, that uncompleted reply popped
back up. Having forgotten everything from this morning, I finished the
reply and sent it...