Hello everyone,
I'm new to wxPython and new to GUI programming in general but, with a
lot of patience and heavy use of the demos, documentation and the
wxPython wiki, I've managed to get most of my problems figured out
eventually. But now I'm stumped and could use a little hand holding
I'm running XP/SP2 using Python 2.4 (ActiveState install) and a very
recent version of wxPython based on wxWidgets 2.6.0.
I have two issues, both of which have to do with capturing keyboard
events. The easy one first
My application has several dialog entry forms, all of which are derived
from wx.Dialog. I would like to capture the ESC key and, in response,
perform the same action as I would when the user clicks on the 'Done' or
'Cancel' button (or whatever button is appropriate for the situation).
The best I can figure out to do is something like this:
Near the end of the __init__() for my derived dialog class I have the line
wx.EVT_CHAR(self, Char)
Then I have a method, Char(self, event), that (for the time being)
does nothing but print "I'm here!" to the console. The trouble is
Char() appears NEVER to be called no matter what keys I press. It
seems like binding the ESC key should be a simple thing to do. I
suspect it is and I'm just missing something. I saw an earlier post
where the poster was having a similar problem but I'm afraid I didn't
comprehend the fix. Anyone want to have another go?
Problem 2:
This is also related to keyboard capture. Like all wxPython
applications (I guess), I derive an application class from wx.App and
that class does little other than instantiating an instance of
MainWindow (a class I wrote derived from wx.Frame), calling
SetTopWindow() on it and making it visible. The code in my derived
MainWindow class represents the bulk of my application. In the
__init__() for MainWindow(), I lay out the widgets that make up my
application interface which, in this case, consists of a handful of
buttons, a few labels and an htmlWindow.
Now I would like to bind keyboard shortcuts to a few of the buttons. I
tried wx.EVT_CHAR() here too and met with the same problem as before --
the Char() method never seems to get called no matter what key I
press. I saw an earlier post where someone suggested using an
AcceleratorTable. I set one up and hey! That mostly works! My
specific trouble has to do with the exact keys I'm trying to bind --
SPACE, BACK and RETURN. This is an unusual set, I know, but this code
is a port from an earlier Python/Tkinter version of the application and
that version used SPACE, BACK and RETURN. If possible, I'd like to
continue using them rather than explaining to people that I was too dumb
to figure out how! :-)
BACK and RETURN handling seems to work exactly like I want. SPACE
however is troublesome. The AcceleratorTable technique picks up the
SPACE event no problem and it does the right thing with it. The trouble
is, whatever button happens to have focus at the time also picks up the
SPACE event and acts on it resulting in two actions -- one wanted, one
unwanted.
How can I prevent the button with focus from acting on the SPACE event?
Is there a way to make the buttons so they can't ever have keyboard
focus in the same way that labels, say, don't get keyboard focus?
(Actually I would prefer that anyway) Alternatively, all my buttons are
instances of my own button class derived from wx.Button. Is there a way
I can capture and ignore the SPACE event if they do have focus?
Thanks,
All suggestions or comments welcome!
Charlie Hubbard
To unsubscribe, e-mail: wxPython-users-unsubscribe (AT) lists (DOT) wxwidgets.org
For additional commands, e-mail: wxPython-users-help (AT) lists (DOT) wxwidgets.org