New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
key_press_event in pyqt4 embedded matplotlib #707
Comments
I am resubmitting the code --- this time properly formatted. 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() |
This might be related to issue 525 |
You need to activate the focus of qt onto your mpl canvas. Just add:
right after you call setParent. Also, I just submitted a pull request #717 which exposes to embedding applications the default mpl keypress handler described at http://matplotlib.sourceforge.net/users/navigation_toolbar.html#navigation-keyboard-shortcuts. I modified your example to use this handler and submitted it as an example: https://github.com/jdh2358/matplotlib/blob/00824666129917301b21404b364879cd4b732495/examples/user_interfaces/embedding_in_qt4_wtoolbar.py |
@jdh2358 has provided a solution for this issue. closing. |
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 main():
app = QApplication(sys.argv)
form = AppForm()
form.show()
app.exec_()
if name == "main":
main()
--code ends--
The text was updated successfully, but these errors were encountered: