5

I have a slight modified version of this example from matplotlib: https://matplotlib.org/gallery/user_interfaces/embedding_in_qt_sgskip.html

The only thing changed is the imports since I am using PySide2, so the imports looks like this:

from PySide2 import QtCore, QtWidgets
from matplotlib.backends.backend_qt5agg import (FigureCanvasQTAgg as FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure

This works fine when running the code in pycharm, or running the scrips by itself, however after an .exe is created with PyInstaller I get the following error:

TypeError: 'PySide2.QtWidgets.QBoxLayout.addWidget' called with wrong argument types:                                     
  PySide2.QtWidgets.QBoxLayout.addWidget(FigureCanvasQTAgg)                                                            
Supported signatures:                                                                                                     
  PySide2.QtWidgets.QBoxLayout.addWidget(PySide2.QtWidgets.QWidget, int=0, 
  PySide2.QtCore.Qt.Alignment=Default(Qt.Alignment))                                                                                                                     
  PySide2.QtWidgets.QBoxLayout.addWidget(PySide2.QtWidgets.QWidget)

It seems as the FigureCanvasQtAgg is no longer recognized as a QWidget, so it can't be added to the layout.

I've tried adding these lines to suggest pyside as suggested here:

os.environ["QT_API"] = "PySide2"
matplotlib.use('Qt5Agg')
matplotlib.rcParams['backend.qt5']='PySide2'

However that does not change the error message of the exe. In pycharm it still runs fine.

EDIT: It seems this is some problem with PySide2+PyInstaller, after replacing this line:

from PySide2 import QtCore, QtWidgets

with this line:

from PyQt5 import QtCore, QtWidgets

It works even after using PyInstaller.

But I want to use PySide2 instead of PyQt5, anyone know a way to solve this?

boking
  • 145
  • 1
  • 12

4 Answers4

3

This thread is a bit old, however, as it was the first hit when I googled for the problem, and this solution is not yet mentioned, I thought I share it:

I had the issue the other way around: I had adapted this live plotting solution to use PySide2, but naturally first tried it with PyQt5. After adaption to PySide2, running the single script failed with the same error as mentioned by OP. However, when importing the window class to another module and creating it there, everything worked fine.

What worked for me was removing PyQt5 from the environment, i.e.

pip uninstall PyQt5

After that, running the script alone worked again.

EDIT: I just revisited my code, and found that changing the order of imports, so that PySide2 is imported prior to matplotlib also did the trick. So it seems that if neither PyQt5 nor PySide2 is already imported, matplotlib checks if PyQt5 is installed and if so, uses this as backend.

Not working:

import matplotlib
from matplotlib.animation import TimedAnimation
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.lines import Line2D

from PySide2 import QtWidgets

working:

from PySide2 import QtWidgets

import matplotlib
from matplotlib.animation import TimedAnimation
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.lines import Line2D
Jakob
  • 226
  • 2
  • 7
2

For PySide2, I have a similar problem when I use the version PySide 5.13.

It is however, working fine for PySide version 5.6.

Interestingly, the issue looks like with the matplotlib wrapper. It seems there is a change in the wrapper for FigureCanvasQTAgg. The one that is working, the wrapper is: Shiboken.ObjectType. While the one that is not working, the wrapper is: sip.wrappertype.

kursun
  • 214
  • 2
  • 23
1

I had the same problem, while using PySide6, on Ubuntu 20.04.

For reference, here is the error I was getting:

TypeError: 'PySide6.QtWidgets.QGraphicsScene.addWidget' called with wrong argument types:
  PySide6.QtWidgets.QGraphicsScene.addWidget(LivePlotFigCanvas)
Supported signatures:
  PySide6.QtWidgets.QGraphicsScene.addWidget(PySide6.QtWidgets.QWidget, PySide6.QtCore.Qt.WindowFlags = Default(Qt.WindowFlags))

Following worked:

  1. Import all PySide6 packages before importing matplotlib packages
  2. Uninstall and install matplotlib

This error doesn't seem to be related to PyInstaller.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
0

I had the same problem. This worked for me

conda uninstall matplotlib
pip install matplotlib
Gj_Rk
  • 1