matplotlib/matplotlib — Issue #707: key_press_event in pyqt4 embedded matplotlib
Author: psaurav
Created: 2012-02-18 23:07:39+00:00
State: open

Matplotlib does not respond to key presses while embedded in PyQt4. I have verified that key presses work with Tkinter. My system: Matplotlib 1.1.0, python 2.6.5 on Ubuntu. The code I have used is given below:

--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.x, self.y = self.get_data()
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--