问题描述
我从matplotlib对该示例进行了少许修改: https://matplotlib.org/gallery/user_interfaces/embedding_in_qt_sgskip.html
I have a slight modified version of this example from matplotlib: https://matplotlib.org/gallery/user_interfaces/embedding_in_qt_sgskip.html
唯一改变的是导入,因为我使用的是 PySide2,所以导入看起来像这样:
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
在 pycharm 中运行代码或单独运行脚本时,这可以正常工作,但是在使用 PyInstaller 创建 .exe 后,我收到以下错误:
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)
似乎FigureCanvasQtAgg不再被视为QWidget,因此无法将其添加到布局中.
It seems as the FigureCanvasQtAgg is no longer recognized as a QWidget, so it can't be added to the layout.
我已经尝试添加这些行来建议 pyside 建议 here:
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'
但是,这不会更改exe的错误消息.在 pycharm 中它仍然运行良好.
However that does not change the error message of the exe. In pycharm it still runs fine.
替换此行后,看来PySide2 + PyInstaller出现了问题:
from PySide2 import QtCore, QtWidgets
用这一行:
from PyQt5 import QtCore, QtWidgets
即使在使用 PyInstaller 后它也能工作.
It works even after using PyInstaller.
但是我想使用PySide2代替PyQt5,有人知道解决此问题的方法吗?
But I want to use PySide2 instead of PyQt5, anyone know a way to solve this?
推荐答案
对于PySide2,当我使用PySide 5.13版本时,我也遇到类似的问题.
For PySide2, I have a similar problem when I use the version PySide 5.13.
然而,它对于 PySide 5.6 版很好.
It is however, working fine for PySide version 5.6.
有趣的是,问题看起来与 matplotlib 包装器有关.FigureCanvasQTAgg
的包装器似乎发生了变化.正在运行的包装器是:Shiboken.ObjectType
.虽然那个不起作用,但包装器是:sip.wrappertype
.
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
.
这篇关于使用PyInstaller后,FigureCanvas未解释为QtWidget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!