Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
Bug summary
This bug is similar to#11607. In my case, I get it when I combine PyQt5 and matplotlib 3.5.1. In any of the other combinations (PyQt5 and matplotlib==3.4.3 or PyQt6 and matplotlib==3.5.1) it works as expected.
Code for reproduction
# change this to use PyQt5 or PyQt6qt='qt5'importsysimportmatplotlibfrommatplotlib.figureimportFigureifqt=='qt6':fromPyQt6.QtWidgetsimport*fromPyQt6.QtCoreimport*fromPyQt6.QtGuiimport*ifmatplotlib.__version__=='3.4.3':raiseValueError('For PyQt6 Install matplotlib 3.5.1')frommatplotlib.backends.backend_qtaggimport (FigureCanvas,NavigationToolbar2QTasNavigationToolbar)else:fromPyQt5.QtWidgetsimport*fromPyQt5.QtCoreimport*frommatplotlib.backends.backend_qt5aggimport (FigureCanvasQTAggasFigureCanvas,NavigationToolbar2QTasNavigationToolbar)importnumpyasnpprint('Matplotlib version:',matplotlib.__version__)classChartsBase(QMdiSubWindow):def__init__(self):super(ChartsBase,self).__init__()self.setMinimumSize(400,400)self.mainwidgetmdi=QMainWindow()# must be QMainWindow to handle the toolbarself.setWidget(self.mainwidgetmdi)fig=Figure()self.figure_canvas=FigureCanvas(fig)self.fig=self.figure_canvas.figureself.mainwidgetmdi.setCentralWidget(self.figure_canvas)# similar to figure canvasself.mpl_toolbar=NavigationToolbar(self.figure_canvas,self)self.mpl_toolbar.setVisible(True)ifqt=='qt6':area=Qt.ToolBarArea.BottomToolBarAreaelse:area=Qt.BottomToolBarAreaself.mainwidgetmdi.addToolBar(area,self.mpl_toolbar)t=np.linspace(0,10,501)self.axes=self.fig.subplots(1,1)line_plot_ax=self.axes.plot(t)classMDIWindow(QMainWindow):count=0def__init__(self):super().__init__()self.mdi=QMdiArea()self.setCentralWidget(self.mdi)bar=self.menuBar()file=bar.addMenu("File")file.addAction("New")file.triggered[QAction].connect(self.add_new)self.setWindowTitle("MDI Application with matplotlib")defadd_new(self,p):ifp.text()=="New":sub=ChartsBase()sub.setWindowTitle("Matplotlib example")self.mdi.addSubWindow(sub)sub.show()if__name__=='__main__':app=QApplication(sys.argv)mdi=MDIWindow()mdi.show()sys.exit(app.exec())
Actual outcome
To reproduce:
- execute the app
- Menu "File"
- "New"
- Maximize the subwindow
When the subwindow is maximized I get this error:
Traceback (most recent call last): File "/home/mario/programs/miniconda/lib/python3.8/site-packages/matplotlib/backends/backend_qt.py", line 262, in enterEvent x, y = self.mouseEventCoords(self._get_position(event))AttributeError: 'QResizeEvent' object has no attribute 'pos'
Expected outcome
The maximized subwindow
Additional information
What are the conditions under which this bug happens? input parameters, edge cases, etc?
Usingmatplotlib==3.5.1
withPyQt5
. Not happen with other combinationsmatplotlib==3.5.1
andPyQt6
ormatplotlib==3.4.3
andPyQt5
Do you know why this bug is happening?
This bug is related to the QEvents methods. QResizeEvent does have not any method to get the position in PyQt5, unlike PyQt6 which does have it
Do you maybe even know a fix?
Add a try-except clausule to this method
matplotlib/lib/matplotlib/backends/backend_qt.py
Lines 264 to 266 in60d0c33
defenterEvent(self,event): | |
x,y=self.mouseEventCoords(self._get_position(event)) | |
FigureCanvasBase.enter_notify_event(self,guiEvent=event,xy=(x,y)) |
defenterEvent(self,event):try:x,y=self.mouseEventCoords(self._get_position(event))exceptAttributeError:# QResizeEvent has no attribute posx=y=NoneFigureCanvasBase.enter_notify_event(self,guiEvent=event,xy=(x,y))
After this, work as expected!
Happy to open a PR if it is needed!
Operating system
Linux Mint 20.2
Matplotlib Version
3.5.1
Matplotlib Backend
QtAgg
Python version
Python 3.8.5
Jupyter version
No response
Installation
pip