结合PyQt和Matplotlib

结合PyQt和Matplotlib,第1张

结合PyQt和Matplotlib

在回答您的问题之前,请允许我提供一些有关如何提出更好问题的提示。您不会得到很多答复,因为您的代码示例太大,并且因为我们不能只复制粘贴执行它并自己重现问题。

下次创建一个最小但完整的代码片段,该片段仍会重现该问题)。如有可能,请使其运行,而无需自己进行任何修改。这样您将获得更多帮助。

首先制作一个程序副本,然后删除所有不会导致该错误的功能,直到您只剩下最少的示例为止。这样,您很有可能自己找到解决方案。如果不是这样,那么一个小示例可以使我们更加专注于该问题,而不必花数百行代码(例如

setupUi()
您示例中的函数,甚至没有执行)。

仍然会产生错误的最小独立程序是:

import sysfrom PyQt4 import QtCore, QtGuiclass Ui_MainWindow(QtGui.QMainWindow):    def __init__(self, parent = None):        super(Ui_MainWindow, self).__init__(parent)        layout = QtGui.QVBoxLayout()        self.setLayout(layout)if __name__ == "__main__":    qApp = QtGui.QApplication(sys.argv)    main_window = Ui_MainWindow()    main_window.show()    sys.exit(qApp.exec_())

如您所见,它与在PyQt中集成matplotlib无关。问题是

QMainWindow
必须有一个中央小部件,该部件充当所有子小部件的容器。该中央窗口小部件具有自己的布局,其布局
QMainWindow
用于工具栏和停靠窗口小部件(请参见http://doc.qt.io/qt-4.8/qmainwindow.html中的插图)。请注意,您应该使用该
addToolbar()
方法添加工具栏。

因此,错误的解决方法是:

import sysfrom PyQt4 import QtCore, QtGuiclass Ui_MainWindow(QtGui.QMainWindow):    def __init__(self, parent = None):        super(Ui_MainWindow, self).__init__(parent)        self.main_widget = QtGui.QWidget(self)        self.setCentralWidget(self.main_widget)        layout = QtGui.QVBoxLayout()        self.main_widget.setLayout(layout)     # The main_widget is a container to which you        # can add your child widgets here...if __name__ == "__main__":    qApp = QtGui.QApplication(sys.argv)    main_window = Ui_MainWindow()    main_window.show()    sys.exit(qApp.exec_())

现在,您的代码示例包含另一个错误,这是@tcaswell为之提供答案的错误。如果将修复程序内置到程序中,将会看到图形画布将最终显示在其自己的单独窗口中。

再次,一个最小的例子是:

import sysfrom PyQt4 import QtCore, QtGuifrom matplotlib.backends.backend_qt4agg import     FigureCanvasQTAgg as FigureCanvasfrom matplotlib.backends.backend_qt4agg import     NavigationToolbar2QT as NavigationToolbarimport matplotlib.pyplot as pltclass Ui_MainWindow(QtGui.QMainWindow):    def __init__(self, parent = None):        super(Ui_MainWindow, self).__init__(parent)        self.figure = plt.figure()        self.canvas = FigureCanvas(self.figure)        # use addToolbar to add toolbars to the main window directly!        self.toolbar = NavigationToolbar(self.canvas, self)        self.addToolBar(self.toolbar)        self.button = QtGui.QPushButton('Plot')        self.lineEditMomentum1 = QtGui.QLineEdit()        self.lineEditMomentum1.setMaximumSize(200, 30)        self.main_widget = QtGui.QWidget(self)        self.setCentralWidget(self.main_widget)        layout = QtGui.QVBoxLayout()        layout.addWidget(self.canvas)        layout.addWidget(self.button)        layout.addWidget(self.lineEditMomentum1)        self.main_widget.setLayout(layout)if __name__ == "__main__":    qApp = QtGui.QApplication(sys.argv)    main_window = Ui_MainWindow()    main_window.show()    sys.exit(qApp.exec_())

在PyQt中组合matplotlib时,根本不要使用

pyplot
模块的类似matlab的接口。直接使用matplotlib类。

变化

plt.figure()
Figure()
像这样:

import sysfrom PyQt4 import QtCore, QtGuifrom matplotlib.backends.backend_qt4agg     import FigureCanvasQTAgg as FigureCanvasfrom matplotlib.backends.backend_qt4agg     import NavigationToolbar2QT as NavigationToolbarfrom matplotlib.figure import Figureclass Ui_MainWindow(QtGui.QMainWindow):    def __init__(self, parent = None):        super(Ui_MainWindow, self).__init__(parent)        self.figure = Figure() # don't use matplotlib.pyplot at all!        self.canvas = FigureCanvas(self.figure)        # use addToolbar to add toolbars to the main window directly!        self.toolbar = NavigationToolbar(self.canvas, self)        self.addToolBar(self.toolbar)        self.button = QtGui.QPushButton('Plot')        self.lineEditMomentum1 = QtGui.QLineEdit()        self.lineEditMomentum1.setMaximumSize(200, 30)        self.main_widget = QtGui.QWidget(self)        self.setCentralWidget(self.main_widget)        layout = QtGui.QVBoxLayout()        layout.addWidget(self.canvas)        layout.addWidget(self.button)        layout.addWidget(self.lineEditMomentum1)        self.main_widget.setLayout(layout)if __name__ == "__main__":    qApp = QtGui.QApplication(sys.argv)    main_window = Ui_MainWindow()    main_window.show()    sys.exit(qApp.exec_())

最后,您可能会考虑在不使用Qt
Designer的情况下创建程序。通过从头开始构建它们,您将获得很多洞见,您的代码将更具可读性,并且使问题的最少示例变得更加容易。我认为键入小部件语句所花费的额外时间将使自己获得回报。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5674601.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存