我正在尝试使用addWidget函数将FigureCanvasQTAgg参数作为窗口小部件添加到某些布局,并且发生TypeError。仅当尝试运行使用pyinstaller构建的独立应用程序时,此错误才适用。直接运行脚本时,一切正常。

我正在使用python=3.6PySide2=5.12.3pyInstaller=3.4

from PySide2.QtWidgets import QApplication, QMainWindow, QMessageBox, QLineEdit, QComboBox, QWidget
import matplotlib
matplotlib.use("Qt5Agg")
from PySide2.QtCore import Qt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure

class TasksConfigCreatorAdapter(QMainWindow):
    def __init__(self):
        super(TasksConfigCreatorAdapter, self).__init__()
        self.dialog = Ui_TasksConfigCreatorDialog()
        self.dialog.setupUi(self)
        self.figure = None
        self.setupCanvasLayout()

    def setupCanvasLayout(self):
        if self.figure is None:
            self.figure = plt.figure()
            self.canvas = FigureCanvas(self.figure)
            self.figure.set_facecolor("white")
            self.toolbar = NavigationToolbar(self.canvas, None)
            self.dialog.canvasLayout.addWidget(self.canvas, *(0, 0))
            self.dialog.canvasLayout.addWidget(self.toolbar, *(1, 0))


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

最佳答案

通过在规范文件的排除列表中添加PyQt5模块来解决此问题。
当尝试在一个文件夹模式下创建独立文件时,我发现添加了PyQt5文件以及PySide2文件,这些文件会中断并导致发生此问题。

关于python - PySide2.QtWidgets.QxxxxLayout.addWidget调用了错误的参数类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56304203/

10-12 16:48