This has been fixed for now!
I am trying to make a GUI using PyQt4 with an embedded matplotlib canvas. When I scroll with the cursor over the canvas I would like to be able to control the behaviour based on an additional key press (control in this case). However, the key attribute of the mouseEvent linked to the 'scroll_event' is always None. I tested that my code correctly registers the key for a mouseEvent generated by 'button_press_event'.
In the example below the on_press method correctly prints the key that was pressed at the same time. Whereas on_scoll always prints None.
How can I get access the key that was pressed during the mouse scroll event?
Thanks in advance!
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import matplotlib
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
class GraphicTool(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.create_menu()
def on_press(self, event):
print event.key
def on_scroll(self, event):
print event.key
if event.key == 'ctrl':
# do something
pass
else:
# do something else
pass
def create_main_frame(self):
self.main_frame = QWidget()
# Create the mpl Figure and FigCanvas objects.
# 10x8 inches, 100 dots-per-inch
self.dpi = 100
self.fig = Figure((10.0, 8.0), dpi=self.dpi)
self.canvas = FigureCanvas(self.fig)
self.canvas.setParent(self.main_frame)
self.canvas.setFocusPolicy(Qt.ClickFocus)
self.canvas.setFocus()
self.axes = self.fig.add_subplot(111)
self.canvas.mpl_connect('scroll_event', self.on_scroll)
self.canvas.mpl_connect('button_press_event', self.on_press)
# Create the navigation toolbar, tied to the canvas
self.mpl_toolbar = NavigationToolbar(self.canvas,
self.main_frame)
vbox = QVBoxLayout()
vbox.addWidget(self.canvas)
vbox.addWidget(self.mpl_toolbar)
self.main_frame.setLayout(vbox)
self.setCentralWidget(self.main_frame)
def main():
app = QApplication(sys.argv)
viewer = GraphicTool()
viewer.show()
viewer.raise_()
app.exec_()
if __name__ == "__main__":
main()
The short example above actually does work as expected. However, when I incorporate it into a bigger project it fails. I will keep debugging to see if any other events are disturbing the scroll event.