key_press_events in matplotlib embedded in pyqt4

Hi,

I am trying to generate key press events in matplotlib (imshow) embedded in pyqt4, but I am getting nowhere. My code is given below. Even though the imshow seems to work OK, there is no response when I press keys.

I am using an mpl_connect
     self.canvas.mpl_connect('key_press_event', self.on_key_press)

and define a function on_key_press:
   def on_key_press(self, event):
         print 'you pressed', event.key

The complete code is given below. I hope someone will show me where I am going wrong. I am running this on Ubuntu, with python 2.6.x and matplotlib 1.1.0.

Thanks for your help!

Saurav

==code begins==
import sys
import numpy as np
from matplotlib.figure import Figure

from PyQt4.QtCore import *
from PyQt4.QtGui import *

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar

class AppForm(QMainWindow):
     def __init__(self, parent=None):
         QMainWindow.__init__(self, parent)
         self.data = self.get_data2()
         self.create_main_frame()
         self.on_draw()

     def create_main_frame(self):
         self.main_frame = QWidget()

         self.fig = Figure((5.0, 4.0), dpi=100)
         self.canvas = FigureCanvas(self.fig)
         self.canvas.setParent(self.main_frame)
         self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame)

         self.canvas.mpl_connect('key_press_event', self.on_key_press)

         vbox = QVBoxLayout()
         vbox.addWidget(self.canvas) # the matplotlib canvas
         vbox.addWidget(self.mpl_toolbar)
         self.main_frame.setLayout(vbox)
         self.setCentralWidget(self.main_frame)

     def get_data2(self):
         return np.arange(20).reshape([4,5]).copy()

     def on_draw(self):
         self.fig.clear()
         self.axes = self.fig.add_subplot(111)
         #self.axes.plot(self.x, self.y, 'ro')
         self.axes.imshow(self.data, interpolation='nearest')
         self.canvas.draw()

     def on_key_press(self, event):
         print 'you pressed', event.key

def main():
     app = QApplication(sys.argv)
     form = AppForm()
     form.show()
     app.exec_()

if __name__ == "__main__":
     main()
==code ends==

Saurav Pathak :

I am trying to generate key press events in matplotlib (imshow) embedded
in pyqt4, but I am getting nowhere.

Yes, another victim...

My small animation test program

http://users.info.unicaen.fr/~karczma/TEACH/Test/isingVZ.py

run under Windows XP / Python 2.7, shows the following

1. For WX and GTK the timing doesn't work properly.
2. For QT4 key event processing doesn't work

Only Ye Olde Tkinter backend seems to behave decently.
Well, the animation in matplotlib - as I have already mentioned some week ago - is really imperfect, and should be re-analysed (in particular not giving to the user the access to the event loop is a severe handycap). Now I see that events in general need to be looked into in details. I feel sorry that I have no time to dig more thoroughly.
(Of course, I may be doing some rubbish..., then mes plus plates excuses).

Jerzy Karczmarczuk

Please file bug reports on these issues, as they are critical.

Ben Root

···

On Saturday, February 18, 2012, Jerzy Karczmarczuk wrote:

Saurav Pathak :

I am trying to generate key press events in matplotlib (imshow) embedded

in pyqt4, but I am getting nowhere.

Yes, another victim…

My small animation test program

http://users.info.unicaen.fr/~karczma/TEACH/Test/isingVZ.py

run under Windows XP / Python 2.7, shows the following

  1. For WX and GTK the timing doesn’t work properly.

  2. For QT4 key event processing doesn’t work

Only Ye Olde Tkinter backend seems to behave decently.

Well, the animation in matplotlib - as I have already mentioned some

week ago - is really imperfect, and should be re-analysed (in particular

not giving to the user the access to the event loop is a severe

handycap). Now I see that events in general need to be looked into in

details. I feel sorry that I have no time to dig more thoroughly.

(Of course, I may be doing some rubbish…, then mes plus plates excuses).

Jerzy Karczmarczuk

I created an issue here:

···

https://github.com/matplotlib/matplotlib/issues/707

  On Saturday, February 18, 2012, Jerzy Karczmarczuk wrote:
    Saurav

Pathak :

    > I am trying to generate key press events in matplotlib

(imshow) embedded

    > in pyqt4, but I am getting nowhere.

    Yes, another victim...



    My small animation test program



    [http://users.info.unicaen.fr/~karczma/TEACH/Test/isingVZ.py](http://users.info.unicaen.fr/%7Ekarczma/TEACH/Test/isingVZ.py)



    run under Windows XP / Python 2.7, shows the following



    1. For WX and GTK the timing doesn't work properly.

    2. For QT4 key event processing doesn't work



    Only Ye Olde Tkinter backend seems to behave decently.

    Well, the animation in matplotlib - as I have already mentioned

some

    week ago - is really imperfect, and should be re-analysed (in

particular

    not giving to the user the access to the event loop is a severe

    handycap). Now I see that events in general need to be looked

into in

    details. I feel sorry that I have no time to dig more

thoroughly.

    (Of course, I may be doing some rubbish..., then mes plus plates

excuses).

    Jerzy Karczmarczuk
    Please file bug reports on these issues, as they are

critical.

Ben Root



Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing also focuses on allowing computing to be delivered as a service.


Matplotlib-users mailing list

http://www.accelacomm.com/jaw/sfnl/114/51521223/


Matplotlib-users@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/matplotlib-users