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==

------------------------------------------------------------------------------
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.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to