Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib 'key_press_event' does not respond

By clicking with mouse button on Figure: self.canvas.Fig.canvas.mpl_connect('button_press_event', self.button_press)

I could receive signal and generate answer by printing "Button pressed" in status bar self.statusBar().showMessage("Key pressed", 400)

But for some reason same piece of code does not work for key (keyboard) pressing: self.canvas.Fig.canvas.mpl_connect('key_press_event', self.key_press)

The message "Key pressed" does not appear, meaning that no event occured or no signal were received.

This is my MWE with all essential classes:

import sys
import matplotlib 
matplotlib.use("Qt5Agg")
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QSizePolicy

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas


class MyMplCanvas(FigureCanvas):
    def __init__(self, parent=None, width=5, height=4, dpi=100,data=[[]],timedelay=[],wavelength=[]):
        self.Fig = Figure(figsize=(width, height), dpi=dpi)
        self.Dataplot = self.Fig.add_subplot(111)

        self.compute_initial_figure(data,timedelay, wavelength)    

        FigureCanvas.__init__(self, self.Fig)
        self.setParent(parent)
        FigureCanvas.setSizePolicy(self,QSizePolicy.Expanding,QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

    def compute_initial_figure(self,data,timedelay,wavelength):
        pass

class MyStaticMplCanvas(MyMplCanvas):  
    def __init__(self, *args, **kwargs):
        MyMplCanvas.__init__(self, *args, **kwargs)

    def compute_initial_figure(self,data,timedelay,wavelength): 
        self.Dataplot.set_xlabel('Wavelength, nm')
        self.Dataplot.set_ylabel('Time delay, ~s')

class GraphView(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.main_widget = QtWidgets.QWidget(self)

        self.canvas = MyStaticMplCanvas(self.main_widget, width=8, height=8, dpi=100,data=[[]],timedelay=[],wavelength=[])

        self.canvas.Fig.canvas.mpl_connect('key_press_event', self.key_press)
        self.canvas.Fig.canvas.mpl_connect('button_press_event', self.button_press)

        self.layoutMain = QtWidgets.QHBoxLayout(self.main_widget)      
        self.layoutFigure = QtWidgets.QHBoxLayout()
        self.layoutMain.addLayout(self.layoutFigure)   
        self.layoutFigure.addWidget(self.canvas)


        self.main_widget.setFocus()
        self.setCentralWidget(self.main_widget)

    def key_press(self,event):
        self.statusBar().showMessage("Key pressed", 400)

    def button_press(self,event):
        self.statusBar().showMessage("Button pressed", 400) 

def main():
    app = QApplication( sys.argv )
    a=GraphView()
    a.show() 
    app.exec()

if __name__ == '__main__':
    sys.exit(main()) 

What is problem with my code?

like image 443
saldenisov Avatar asked Jul 27 '15 14:07

saldenisov


1 Answers

i found a solution here

after

self.canvas = MyStaticMplCanvas(self.main_widget, width=8, height=8, dpi=100,data=[[]],timedelay=[],wavelength=[])

add

self.canvas.setFocusPolicy( QtCore.Qt.ClickFocus )
self.canvas.setFocus()

click in the canvas so it has the focus, from then you will see the key_presses

like image 87
a_manthey_67 Avatar answered Oct 19 '22 10:10

a_manthey_67