我的项目有一个类GUI
,它使用PySide将Matplotlib的FigureCanvas嵌入QGridLayout中的两个单元格之一。另一个类是MplGrapher
,它最初是FigureCanvas的扩展。起初,一切都工作正常,除了情节没有导航工具栏。然后我找到了一个教程,解释说如果你想要一个带有嵌入式绘图的导航工具栏,你需要先创建一个实例...所以在此之后,我的绘图包括一个工具栏,但它的尺寸也减小了我可以'让它填充GUI's
QGridLayout的单元格。我试过让MplGrapher
类扩展QWidget而不是......我已经尝试在MplGrapher
内创建一个包含绘图和工具栏的子布局......我已经尝试过设置QSizePolicy,但是似乎没什么用。
让我这么困难的一部分原因是我不确定创建FigureCanvas的正确方法,我不确定创建NavigationToolbar的正确方法。扩展,初始化和设置此类的父级MplGrapher
有太多不同的方法,我正在忘记我的测试用例,以便自己解决问题。令人困惑的是,所有这些不同的测试用例只产生三种结果之一:
GUI
类的QMainWindow消失(没有崩溃)以下是一些代码:
#FigureCanvas to be embedded in PySide GUI
class MplGrapher(FigureCanvas):
def __init__(self,name,parent):
self.figure = Figure()
self.figure.suptitle(name)
self.parent = parent
super(FigureCanvas, self).__init__(self.figure)
self.initFigure()
def initFigure(self):
self.plt = self.figure.add_subplot(111)
self.mpl_toolbar = NavigationToolbar(self, self.parent)
这是另一次尝试:
# FigureCanvas to be embedded in PySide GUI
class MplGrapher(QWidget):
def __init__(self,name,parent):
self.figure = Figure()
self.figure.suptitle(name)
self.parent = parent
super(MplGrapher, self).__init__(self.parent)
self.initFigure()
def initFigure(self):
self.plt = self.figure.add_subplot(111)
self.mpl_toolbar = NavigationToolbar(self, self.parent)
另一个:
# FigureCanvas to be embedded in PySide GUI
class MplGrapher(FigureCanvas):
def __init__(self,name,parent):
self.figure = Figure()
self.figure.suptitle(name)
self.parent = parent
super(MplGrapher, self).__init__(self.figure)
self.initFigure()
def initFigure(self):
self.setParent(self.parent)
self.plt = self.figure.add_subplot(111)
self.mpl_toolbar = NavigationToolbar(self, self.parent)
另一个:
# FigureCanvas to be embedded in PySide GUI
class MplGrapher(FigureCanvas):
def __init__(self,name,parent=None):
super(FigureCanvas, self).__init__(Figure())
self.initFigure(name)
def initFigure(self, name):
self.layout = QGridLayout()
self.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Preferred)
self.figure = Figure()
self.figure.suptitle(name)
self.canvas = FigureCanvas(self.figure)
self.canvas.setParent(self)
self.plt = self.figure.add_subplot(111)
self.mpl_toolbar = NavigationToolbar(self.canvas, self)
self.layout.addWidget(self.canvas,0,0)
self.layout.addWidget(self.mpl_toolbar,1,0)
答案 0 :(得分:2)
这样的事情应该做你想做的事。
class MplGrapher(QtGui.QWidget):
def __init__(self,name,parent=None):
super(MplGrapher, self).__init__(parent)
self.initFigure(name)
def initFigure(self, name):
self.figure = Figure()
self.canvas = FigureCanvas(self.figure)
self.plt = self.figure.add_subplot(111)
self.navbar = NavigationToolbar(self.canvas, self)
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.navbar)
self.layout.addWidget(self.canvas)
self.setLayout(self.layout)