我一直在寻找一个工作示例,如何在pyside中嵌入一个matplotlib图,它是与QT设计器一起创建的,同时将逻辑保存在一个单独的文件中。我知道web上有很多示例,但它们中没有一个实际使用QT设计器,然后创建一个单独的文件来将matplitlib图添加到小部件中的逻辑。我找到了一个“几乎”工作http://blog.rcnelson.com/building-a-matplotlib-gui-with-qt-designer-part-1/的例子,但在我的版本中,“将layoutName属性从”verticalLayout“更改为”mplvl“是不可能的。
因此,我有以下具体问题:我不清楚在Pyside Qt设计器中可以嵌入哪些项目。它是一个简单的“小部件”(因为pyside中没有matplotlib小部件可用)。如果是这样的话,我如何才能将情节添加到该小部件中?还是我必须用Qt设计器创建一个“FigureCanvas”?这有可能吗?如果是这样的话,是怎么做的?
这里是我可以用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对象添加一个绘图?
发布于 2016-04-17 08:14:48
我不是这方面的专家,所以这可能不是最干净的方法,但是下面是一些可以让您开始的工作代码:
QxxxxLayoutPlotter继承了FigureCanvasmatplotlib和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,使其大小和比例正确,因此您应该能够得到您想要的这个例子或另一个。
祝好运!
发布于 2016-04-20 07:04:28
看看嵌入在QT4:qt4.html中的matplotlib示例
在这里,他们定义了几个在QT4应用程序中实现matplotlib小部件的类,例如类MyStaticMplCanvas。在QT设计器中使用这个类的诀窍是使用一个标准QWidget,右键单击它并选择Promote to ...填充Promoted class name下的类名MyStaticMplCanvas,以及在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 MyStaticMplCanvashttps://stackoverflow.com/questions/36464357
复制相似问题