Some keys generate tracebacks in the GTK backend

Hi,

Pressing tab, the "Windows" key or the right click key (and maybe others) on a plot with the GTKAgg or GTK backend causes the following traceback:

Traceback (most recent call last):
   File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_gtk.py", line 264, in key_press_event
     FigureCanvasBase.key_press_event(self, key, guiEvent=event)
   File "/usr/lib/python2.5/site-packages/matplotlib/backend_bases.py", line 1459, in key_press_event
     self.callbacks.process(s, event)
   File "/usr/lib/python2.5/site-packages/matplotlib/cbook.py", line 259, in process
     proxy(*args, **kwargs)
   File "/usr/lib/python2.5/site-packages/matplotlib/cbook.py", line 185, in __call__
     return mtd(*args, **kwargs)
   File "/usr/lib/python2.5/site-packages/matplotlib/backend_bases.py", line 2079, in key_press
     if event.key in fullscreen_keys:
TypeError: 'in <string>' requires string as left operand

This happens because in these cases the key is None. The WXAgg backend doesn't have this bug (I haven't tested qt nor tk backends).

Debian Squeeze (testing) using Python 2.5 and the latest mpl SVN (rev 8360).

Regards,
Jo�o Silva

Hi,

Pressing tab, the “Windows” key or the right click key (and maybe
others) on a plot with the GTKAgg or GTK backend causes the following
traceback:

Traceback (most recent call last):

[snip]

File “/usr/lib/python2.5/site-packages/matplotlib/backend_bases.py”,
line 2079, in key_press
if event.key in fullscreen_keys:
TypeError: ‘in ’ requires string as left operand

This happens because in these cases the key is None. The WXAgg backend
doesn’t have this bug (I haven’t tested qt nor tk backends).

This is similar to an issue I had. I posted a patch for my error, but it was specific to Qt4 and required all keys to be manually added to the class definition. I suggested a more robust solution: returning early in backend_bases when the key is None. That suggestion kind of died after Darren Dale solicited responses from other devs.

If it helps at all, I’ve attached a very simple patch. The early return at the top of the patch is the important part. The change near the bottom is added because we’ve already checked for None (i.e. purely cosmetic).

-Tony

Index: lib/matplotlib/backend_bases.py

···

On Jun 1, 2010, at 8:59 AM, João Luís Silva wrote:

===================================================================

— lib/matplotlib/backend_bases.py (revision 8366)

+++ lib/matplotlib/backend_bases.py (working copy)

@@ -2062,6 +2062,8 @@

self.destroy() # how cruel to have to destroy oneself!

return

  •    if event.key is None:
    
  •        return
    

Load key-mappings from your matplotlibrc file.

fullscreen_keys = rcParams[‘keymap.fullscreen’]

home_keys = rcParams[‘keymap.home’]

@@ -2128,8 +2130,7 @@

ax.set_xscale(‘log’)

ax.figure.canvas.draw()

  •    elif event.key is not None and \
    
  •             (event.key.isdigit() and event.key!='0') or event.key in all:
    
  •    elif (event.key.isdigit() and event.key!='0') or event.key in all:
    

keys in list ‘all’ enables all axes (default key ‘a’),

otherwise if key is a number only enable this particular axes

if it was the axes, where the event was raised

Thanks Tony, committed to svn r8367

JDH

···

On Wed, Jun 2, 2010 at 8:28 AM, Tony S Yu <tsyu80@...149...> wrote:

On Jun 1, 2010, at 8:59 AM, João Luís Silva wrote:

Hi,

Pressing tab, the "Windows" key or the right click key (and maybe
others) on a plot with the GTKAgg or GTK backend causes the following
traceback: