我正在创建一个也使用 MatPlotLib 的简单 PySide 应用程序。但是,当我将图形添加到 QFrame 中时,图形不会自动适合框架:

我的图表是使用以下代码创建的:

class GraphView(gui.QWidget):
    def __init__(self, name, title, graphTitle, parent = None):
        super(GraphView, self).__init__(parent)

        self.name = name
        self.graphTitle = graphTitle

        self.dpi = 100
        self.fig = Figure((5.0, 3.0), dpi = self.dpi, facecolor = (1,1,1), edgecolor = (0,0,0))
        self.axes = self.fig.add_subplot(111)
        self.canvas = FigureCanvas(self.fig)

        self.Title = gui.QLabel(self)
        self.Title.setText(title)

        self.layout = gui.QVBoxLayout()
        self.layout.addStretch(1)
        self.layout.addWidget(self.Title)
        self.layout.addWidget(self.canvas)
        self.setLayout(self.layout)

    def UpdateGraph(self, data, title = None):
        self.axes.clear()
        self.axes.plot(data)
        if title != None:
            self.axes.set_title(title)

        self.canvas.draw()

它被添加到主 Widget 中,如下所示:
# Create individual Widget/Frame (fftFrame)
        fftFrame = gui.QFrame(self)
        fftFrame.setFrameShape(gui.QFrame.StyledPanel)
        self.FFTGraph = GraphView('fftFrame', 'FFT Transform:', 'FFT Transform of Signal', fftFrame)

最佳答案

这是一个工作代码示例,向您展示了如何让它工作。我首先认为这是因为您在布局中添加了拉伸(stretch),这将占用其他小部件周围的额外空间。但是当我删除它时,它仍然不会调整大小。 “简单”的解决方案是添加一个 resizeEvent,它可以让您定义 GraphView 小部件的大小。在这种情况下,我只是将其几何形状设置为 QFrame 的几何形状,尽管您可能想要添加一些填充并确保为 QFrame 设置合理的最小尺寸。

from PySide import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import sys

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.fft_frame = FftFrame(self)

        self.layout = QtGui.QVBoxLayout()
        self.layout.addWidget(self.fft_frame)
        self.setLayout(self.layout)
        self.setCentralWidget(self.fft_frame)

class FftFrame(QtGui.QFrame):
    def __init__(self, parent=None):
        super(FftFrame, self).__init__(parent)
        self.setFrameShape(QtGui.QFrame.StyledPanel)
        self.parent = parent
        self.graph_view = GraphView('fftFrame', 'FFT Transform:', 'FFT Transform of Signal', self)

    def resizeEvent(self, event):
        self.graph_view.setGeometry(self.rect())


class GraphView(QtGui.QWidget):
    def __init__(self, name, title, graph_title, parent = None):
        super(GraphView, self).__init__(parent)

        self.name = name
        self.graph_title = graph_title

        self.dpi = 100
        self.fig = Figure((5.0, 3.0), dpi = self.dpi, facecolor = (1,1,1), edgecolor = (0,0,0))
        self.axes = self.fig.add_subplot(111)
        self.canvas = FigureCanvas(self.fig)
        self.canvas.setParent(self)

        self.Title = QtGui.QLabel(self)
        self.Title.setText(title)

        self.layout = QtGui.QVBoxLayout()
        self.layout.addWidget(self.Title)
        self.layout.addWidget(self.canvas)
        self.layout.setStretchFactor(self.canvas, 1)
        self.setLayout(self.layout)
        self.canvas.show()

    def update_graph(self, data, title = None):
        self.axes.clear()
        self.axes.plot(data)
        if title != None:
            self.axes.set_title(title)

        self.canvas.draw()


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()

关于python-3.x - 如何在 PySide QFrame 中自动拟合 Matplotlib 图形?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17928629/

10-12 23:24