我一直在寻找一个工作示例,如何在pyside中嵌入matplotlib图,该图是使用QT设计器创建的,同时将逻辑保存在单独的文件中。我知道网上有很多例子,但它们都没有实际使用QT设计器,然后创建一个单独的文件来添加matplitlib图添加到窗口小部件的逻辑。我找到了一个几乎'工作http://blog.rcnelson.com/building-a-matplotlib-gui-with-qt-designer-part-1/,但在我的版本中,它不可能"将layoutName属性从“verticalLayout”更改为“mplvl”"。
所以我有以下具体问题: 我不清楚Pyside Qt设计师可以嵌入哪个项目。这是一个简单的小部件" (因为pyside中没有matplotlib小部件可用)。如果是这样,我怎么才能将情节添加到该小部件?或者我必须创建一个' FigureCanvas'与Qt设计师?这有可能吗?如果是这样,怎么样?
这是Pyside Qt设计师在嵌入小部件时可以做的最简单的设计(这是正确的吗?)。我现在如何在它上面添加一个matplotlib图?
正如其中一个答案中所建议的,我现在将Qwidget提升为MyStaticMplCanvas,并将Qwidget的名称编辑为mplvl。
使用Pyside Qt设计器自动生成的文件并使用 pyside-uic ui.ui -o ui.py -x
ui.py看起来像这样:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'gui.ui'
#
# Created: Wed Apr 20 14:00:02 2016
# by: pyside-uic 0.2.15 running on PySide 1.2.2
#
# WARNING! All changes made in this file will be lost!
from PySide import QtCore, QtGui
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(444, 530)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.mplvl = MyStaticMplCanvas(self.centralwidget)
self.mplvl.setGeometry(QtCore.QRect(120, 190, 221, 161))
self.mplvl.setObjectName("mplvl")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 444, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
from mystaticmplcanvas import MyStaticMplCanvas
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我现在如何从单独的.py文件中将一个图添加到mplvl对象中?
答案 0 :(得分:7)
我不是这方面的专家,所以它可能不是最干净的方法,但这里有一些工作代码可以帮助你入门:
QxxxxLayout
Plotter
FigureCanvas
matplotlib
使用PySide
ui.py:
from PySide import QtCore, QtGui
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(533, 497)
self.mplvl = QtGui.QWidget(Form)
self.mplvl.setGeometry(QtCore.QRect(150, 150, 251, 231))
self.mplvl.setObjectName("mplvl")
self.vLayout = QtGui.QVBoxLayout()
self.mplvl.setLayout(self.vLayout)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
为此,您只需将画布添加到mplvl
QtDesigner
即可
main.py:
import matplotlib
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4'] = 'PySide'
from matplotlib.backends.backend_qt4agg import (
FigureCanvasQTAgg as FigureCanvas,
NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure
from PySide import QtGui, QtCore
import random
from weakref import proxy
from ui import Ui_Form
class Plotter(FigureCanvas):
def __init__(self, parent):
''' plot some random stuff '''
self.parent = proxy(parent)
# random data
data = [random.random() for i in range(10)]
fig = Figure()
super(Plotter,self).__init__(fig)
# create an axis
self.axes = fig.add_subplot(111)
# discards the old graph
self.axes.hold(False)
# plot data
self.axes.plot(data, '*-')
def binding_plotter_with_ui(self):
self.parent.vLayout.insertWidget(1, self)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
# plotter logic and binding needs to be added here
plotter = Plotter(ui)
plotter.binding_plotter_with_ui()
plotter2 = Plotter(ui)
plotter2.binding_plotter_with_ui()
Form.show()
sys.exit(app.exec_())
现在剩下的可能是调整FigureCanvas
以使其尺寸和比例合适,因此您应该能够获得您想要的this example或the other。< / p>
祝你好运!
答案 1 :(得分:3)
看看在QT4中嵌入的matplotlib示例:http://matplotlib.org/examples/user_interfaces/embedding_in_qt4.html
这里他们定义了几个在QT4应用程序中实现matplotlib小部件的类,例如班级MyStaticMplCanvas
。在QT Designer中使用此类的技巧是使用标准QWidget,右键单击它并选择Promote to ...
填写MyStaticMplCanvas
下的类名Promoted class name
,以及此类的文件名在header file
下找到(扩展.h已添加,但在python代码中被忽略)。点击Add
和Promote
。
现在经过uic编译后,python代码看起来应该类似于:
from PySide import QtCore, QtGui
class Ui_Form(object):
def setupUi(self, Form):
...
self.mplvl = MyStaticMplCanvas(Form)
...
from mystaticmplcanvas import MyStaticMplCanvas