[matplotlib-devel] Direction keys not recognized in Qt4 backend

2010-05-14 Thread Tony S Yu
I'm running the Qt4 backend, and I noticed that I'd frequently get error 
messages saying I was pressing unrecognized keys. It turns out that the 
direction keys aren't recognized in the qt4 backend. (I'm using direction keys 
to switch between spaces in OSX, so this error gets triggered quite frequently)

One possible fix is to just add the direction keys to the list of valid keys 
(see patch below). Alternatively, the key event code could just ignore 
unrecognized keys (i.e. `key == None`). This change could be made in 
FigureCanvasQT.keyPressEvent (in backends.backend_qt4.py) or, more generally, 
in FigureCanvasBase.key_press_event (in backend_bases.py).

-Tony

%---Diff

Index: lib/matplotlib/backends/backend_qt4.py
===
--- lib/matplotlib/backends/backend_qt4.py  (revision 8315)
+++ lib/matplotlib/backends/backend_qt4.py  (working copy)
@@ -129,6 +129,10 @@
 keyvald = { QtCore.Qt.Key_Control : 'control',
 QtCore.Qt.Key_Shift : 'shift',
 QtCore.Qt.Key_Alt : 'alt',
+QtCore.Qt.Key_Up : 'up',
+QtCore.Qt.Key_Right : 'right',
+QtCore.Qt.Key_Down : 'down',
+QtCore.Qt.Key_Left : 'left',
}
 # left 1, middle 2, right 3
 buttond = {1:1, 2:3, 4:2}


%---Full traceback

Traceback (most recent call last):
  File "/Users/Tony/python/devel/mpl/lib/matplotlib/backends/backend_qt4.py", 
line 198, in keyPressEvent
FigureCanvasBase.key_press_event( self, key )
  File "/Users/Tony/python/devel/mpl/lib/matplotlib/backend_bases.py", line 
1459, in key_press_event
self.callbacks.process(s, event)
  File "/Users/Tony/python/devel/mpl/lib/matplotlib/cbook.py", line 169, in 
process
func(*args, **kwargs)
  File "/Users/Tony/python/devel/mpl/lib/matplotlib/backend_bases.py", line 
2079, in key_press
if event.key in fullscreen_keys:
TypeError: 'in ' requires string as left operand, not NoneType


--

___
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel


Re: [matplotlib-devel] Direction keys not recognized in Qt4 backend

2010-05-14 Thread Darren Dale
On Fri, May 14, 2010 at 2:39 PM, Tony S Yu  wrote:
> I'm running the Qt4 backend, and I noticed that I'd frequently get error 
> messages saying I was pressing unrecognized keys. It turns out that the 
> direction keys aren't recognized in the qt4 backend. (I'm using direction 
> keys to switch between spaces in OSX, so this error gets triggered quite 
> frequently)
>
> One possible fix is to just add the direction keys to the list of valid keys 
> (see patch below). Alternatively, the key event code could just ignore 
> unrecognized keys (i.e. `key == None`). This change could be made in 
> FigureCanvasQT.keyPressEvent (in backends.backend_qt4.py) or, more generally, 
> in FigureCanvasBase.key_press_event (in backend_bases.py).
>
> -Tony
>
> %---Diff
>
> Index: lib/matplotlib/backends/backend_qt4.py
> ===
> --- lib/matplotlib/backends/backend_qt4.py      (revision 8315)
> +++ lib/matplotlib/backends/backend_qt4.py      (working copy)
> @@ -129,6 +129,10 @@
>     keyvald = { QtCore.Qt.Key_Control : 'control',
>                 QtCore.Qt.Key_Shift : 'shift',
>                 QtCore.Qt.Key_Alt : 'alt',
> +                QtCore.Qt.Key_Up : 'up',
> +                QtCore.Qt.Key_Right : 'right',
> +                QtCore.Qt.Key_Down : 'down',
> +                QtCore.Qt.Key_Left : 'left',
>                }
>     # left 1, middle 2, right 3
>     buttond = {1:1, 2:3, 4:2}
>
>
> %---Full traceback
>
> Traceback (most recent call last):
>  File "/Users/Tony/python/devel/mpl/lib/matplotlib/backends/backend_qt4.py", 
> line 198, in keyPressEvent
>    FigureCanvasBase.key_press_event( self, key )
>  File "/Users/Tony/python/devel/mpl/lib/matplotlib/backend_bases.py", line 
> 1459, in key_press_event
>    self.callbacks.process(s, event)
>  File "/Users/Tony/python/devel/mpl/lib/matplotlib/cbook.py", line 169, in 
> process
>    func(*args, **kwargs)
>  File "/Users/Tony/python/devel/mpl/lib/matplotlib/backend_bases.py", line 
> 2079, in key_press
>    if event.key in fullscreen_keys:
> TypeError: 'in ' requires string as left operand, not NoneType

I can commit the change to backend_qt4, but I need to run the change
to backendbases by the other devs. Is there any reason not to do the
following?:

===
--- lib/matplotlib/backend_bases.py (revision 8306)
+++ lib/matplotlib/backend_bases.py (working copy)
@@ -1,4 +1,3 @@
-
 """
 Abstract base classes define the primitives that renderers and
 graphics contexts must implement to serve as a matplotlib backend
@@ -1450,9 +1449,11 @@

 def key_press_event(self, key, guiEvent=None):
 """
-This method will be call all functions connected to the
+This method will call all functions connected to the
 'key_press_event' with a :class:`KeyEvent`
 """
+if key is None:
+return
 self._key = key
 s = 'key_press_event'
 event = KeyEvent(s, self, key, self._lastx, self._lasty,
guiEvent=guiEvent)

--

___
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel


[matplotlib-devel] GtkAgg Backend and Threads

2010-05-14 Thread Dennis Muhlestein
We have an app that creates some background threads and is doing work at 
the same time it displays a pylab figure.  We noticed the worker threads 
stopped working while we're looking at the figure and then things start 
working again after we close it.

Turns out, from the faq [1], you can have background threads with GtkAgg 
if you call gobject.threads_init().

I changed my matplotlib/backends/backend_gtk.py to:

try:
 import gobject
 import gtk; gdk = gtk.gdk
 import pango
 gobject.threads_init()
except ImportError:
 raise ImportError("Gtk* backend requires pygtk to be installed.")

Sure enough, I can now have my worker threads continue in the background 
while we're watching the figure.  I'm not sure if this should be the 
default behavior or if there ought to be a way to customize the backend 
for individual usage.

A side note however:  We run the same app with a wxPython frontend 
sometimes.  In this mode, the backend is WxAgg.  The Wx backend does not 
have the same default behavior as the GTK backend.  Our worker threads 
continue to do work properly with this backend.  I tried PyQt with a 
small test app as well.  It appears only GTK blocked everything so I 
think perhaps this change should be applied.

-Dennis

[1] http://bit.ly/buQROp


--

___
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel