[pygtk] Key event handling and Pythons lake of switch statement

David Ripton dripton at ripton.net
Mon Jan 28 04:36:02 WST 2013


On 01/27/2013 03:24 PM, Christian Mallwitz wrote:

> Where in C you would have something along the lines of:
>
>   switch (event->keyval) {
>     case GDK_Left: ...
>     case GDK_BackSpace: ...
>     ...
> }
>
> What is your preferred code style to handle that in Python? How about
> more than one keyval triggering the same action?

If it's a small number of cases, just use if/elif.  (With "or" for the 
multiple value case.)

If it's a large number of cases, you can use a dict, with functions or 
methods as the values.

-- 
David Ripton    dripton at ripton.net


More information about the pygtk mailing list