将matplotlib小部件添加到pyqt4中时出错

时间:2014-01-22 02:03:51

标签: python qt matplotlib widget qt-designer

我尝试使用以下代码

将自定义窗口小部件添加到qtdesginer中
from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
import PySide
from matplotlib.figure import Figure

class MplCanvas(FigureCanvas):

    def __init__(self):
        self.fig = Figure()
        self.ax = self.fig.add_subplot(111)

        FigureCanvas.__init__(self, self.fig)
        FigureCanvas.setSizePolicy(self, QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)


class MplWidget(QtGui.QWidget):
    def __init__(self, parent = None):

        QtGui.QWidget.__init__(self, parent)
        self.canvas = MplCanvas()
        self.vbl = QtGui.QVBoxLayout()
        self.vbl.addWidget(self.canvas)
        self.setLayout(self.vbl)

但我只是给了我一个错误

TypeError:使用错误的参数类型调用'PySide.QtGui.QWidget.setSizePolicy':   PySide.QtGui.QWidget.setSizePolicy(Policy,Policy) 支持的签名:   PySide.QtGui.QWidget.setSizePolicy(PySide.QtGui.QSizePolicy)   PySide.QtGui.QWidget.setSizePolicy(PySide.QtGui.QSizePolicy.Policy,PySide.QtGui.QSizePolicy.Policy)

我不确定导致错误的原因,因为我基本上遵循了这一部分http://packtlib.packtpub.com/library/9781847197900/ch06lvl1sec04

任何建议都会很好,因为我是这个qt设计师的新手。

1 个答案:

答案 0 :(得分:0)

我很幸运在我的自定义matplotlib窗口小部件而不是super(...).__init__()方法中继承了使用BaseClass.__init__(self)的FigureCanvas。您的小部件为我工作了一些小的改动:

class MplCanvas(FigureCanvas):
    def __init__(self):
        self.fig = Figure()
        self.ax = self.fig.add_subplot(111)
        super(MplCanvas, self).__init__(self.fig)
        self.setSizePolicy(QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding)
        self.updateGeometry()

另外......我同意@tcaswell您应该选择PyQt4或PySide并避免同时导入:)