1. <tfoot id='pCbN1'></tfoot>
      <bdo id='pCbN1'></bdo><ul id='pCbN1'></ul>

    1. <legend id='pCbN1'><style id='pCbN1'><dir id='pCbN1'><q id='pCbN1'></q></dir></style></legend>

      <i id='pCbN1'><tr id='pCbN1'><dt id='pCbN1'><q id='pCbN1'><span id='pCbN1'><b id='pCbN1'><form id='pCbN1'><ins id='pCbN1'></ins><ul id='pCbN1'></ul><sub id='pCbN1'></sub></form><legend id='pCbN1'></legend><bdo id='pCbN1'><pre id='pCbN1'><center id='pCbN1'></center></pre></bdo></b><th id='pCbN1'></th></span></q></dt></tr></i><div id='pCbN1'><tfoot id='pCbN1'></tfoot><dl id='pCbN1'><fieldset id='pCbN1'></fieldset></dl></div>

      <small id='pCbN1'></small><noframes id='pCbN1'>

    2. 使用 Qt Designer 表单和 PyQt5 在 QWidget 中绘制 matp

      时间:2023-08-07
        <tbody id='B5PWJ'></tbody>

          • <small id='B5PWJ'></small><noframes id='B5PWJ'>

          • <i id='B5PWJ'><tr id='B5PWJ'><dt id='B5PWJ'><q id='B5PWJ'><span id='B5PWJ'><b id='B5PWJ'><form id='B5PWJ'><ins id='B5PWJ'></ins><ul id='B5PWJ'></ul><sub id='B5PWJ'></sub></form><legend id='B5PWJ'></legend><bdo id='B5PWJ'><pre id='B5PWJ'><center id='B5PWJ'></center></pre></bdo></b><th id='B5PWJ'></th></span></q></dt></tr></i><div id='B5PWJ'><tfoot id='B5PWJ'></tfoot><dl id='B5PWJ'><fieldset id='B5PWJ'></fieldset></dl></div>

            1. <tfoot id='B5PWJ'></tfoot>
              <legend id='B5PWJ'><style id='B5PWJ'><dir id='B5PWJ'><q id='B5PWJ'></q></dir></style></legend>
                <bdo id='B5PWJ'></bdo><ul id='B5PWJ'></ul>

              • 本文介绍了使用 Qt Designer 表单和 PyQt5 在 QWidget 中绘制 matplotlib 图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我不明白将 matplotlib 图形链接到从 Qt Designer 创建的表单的最佳方法.我有一个我在 QtDesigner 中创建的表单,然后通过 pyuic5 编译为 python.我的主要程序是:

                I don't understand the best way to link a matplotlib figure to a form created from Qt Designer. I have a form I created in QtDesigner and then compiled to python through pyuic5. My main program is:

                import app_framework as af
                import matplotlib
                from PyQt5 import QtWidgets
                import sys
                
                matplotlib.use('Qt5Agg')
                
                app = QtWidgets.QApplication(sys.argv)
                form = af.MyApp()
                form.show()
                app.exec_()
                

                myApp 调用从 Qt Designer 创建然后由 pyuic5 (design.py) 转换的 app_framework.py 表单:

                where myApp calls the app_framework.py form created from Qt Designer then converted by pyuic5 (design.py):

                from PyQt5.QtWidgets import QApplication, QMainWindow
                import design
                
                class MyApp(QMainWindow, design.Ui_mainWindow):
                   def __init(self):
                       super(self.__class__, self).__init__()
                       <button initializations>
                       <function definitions for button callbacks>
                

                我很困惑在这个框架中我可以将 matplotlib 图形链接到 QtDesigner 中的预制空小部件或类似的东西,以便我可以在发生事情时在 GUI 窗口中绘制新数据(输入的文本、按钮按下等)

                I'm confused as to where in this framework I can link a matplotlib figure to a premade empty widget in QtDesigner, or something of that sort, so that I can plot new data in the GUI window as things happen (text entered, button push, etc.)

                我在这里找到了一些线程 SO 和 matplotlib 的站点,但我不确定我是否理解为此创建空间的正确过程Qt Designer 表单中的小部件,然后链接绘图,和/或创建小部件事后,然后链接和绘图.

                I've found some threads here on SO and matplotlib's site, but I'm not sure I understand the correct process for creating the space for this widget in the Qt Designer form then linking a plot, and/or creating a widget post hoc and then linking and plotting.

                到目前为止,我所做的是在 Qt Creator 中创建一个空的 QWidget,然后在 pyuic5 编译后,我将 design.py 文件更改如下:

                What I've done so far is create an empty QWidget inside Qt Creator and then after pyuic5 compile, I alter the design.py file as follows:

                from PyQt5 import QtCore, QtGui, QtWidgets
                
                # **** ADDED THIS
                from matplotlib.figure import Figure
                from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as Canvas
                # **** 
                
                class Ui_mainWindow(object):
                    def setupUi(self, mainWindow):
                        <mainWindow setup stuff>
                        self.centralwidget = QtWidgets.QWidget(mainWindow)
                
                        # ****ALTERED THIS FROM self.plotWidget = QtWidgets.QWidget(self.centralWidget)
                        self.plotWidget = MplWidget(self.centralWidget)
                        # ***** 
                
                        self.plotWidget.setGeometry(QtCore.QRect(20, 250, 821, 591))
                        self.plotWidget.setObjectName("plotWidget")
                
                  # **** ADDED THIS 
                class MplCanvas(Canvas):
                    def __init__(self):
                        self.fig = Figure()
                        self.ax = self.fig.add_subplot(111)
                        Canvas.__init__(self, self.fig)
                        Canvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
                        Canvas.updateGeometry(self)
                
                
                class MplWidget(QtWidgets.QWidget):
                    def __init__(self, parent=None):
                        QtWidgets.QWidget.__init__(self, parent)
                        self.canvas = MplCanvas()
                 # ***********
                

                然后 app_framework.py 为:

                and then app_framework.py as:

                from PyQt5.QtWidgets import QApplication, QMainWindow
                import design
                
                class MyApp(QMainWindow, design.Ui_mainWindow):
                   def __init(self):
                       super(self.__class__, self).__init__()
                       self.setupUi(self)
                       self.pushButton_plotData.clicked.connect(self.plot_data)
                
                    def plot_data(self):
                        x=range(0, 10)
                        y=range(0, 20, 2)
                        self.plotWidget.canvas.ax.plot(x, y)
                        self.plotWidget.canvas.draw()
                

                我认为这会起作用,但是当我单击情节按钮时,什么也没有发生.它没有锁定,它只是不绘制任何东西.我猜我错过了在这个空小部件中绘制 matplotlib 图形和/或画布的基本知识.

                I thought this would work, but when I click the plot push button, nothing happens. It doesn't lock up, it just doesn't plot anything. I'm guessing I'm missing something fundamental for plotting a matplotlib figure and/or canvas in this empty widget.

                推荐答案

                通过这个SO post,链接到这个 我可能需要购买的一本书的免费示例章节.正如许多其他帖子中提到的那样,需要使用标头 mplwidget 将空白 QtWidget提升"为 MplWidget.完成此操作后,然后运行 ​​pyuic5 命令,from mplwidget import MplWidget"将出现在 design.py 文件中,可以随时更新,无需担心覆盖.然后,创建一个 mplwidget.py 文件:

                I found the solution through the help of this SO post, which links to this free sample chapter of a book I'm probably gonna need to buy. As sort of mentioned in a number of other posts, one needs to "Promote" the blank QtWidget to a MplWidget using the header mplwidget. After doing this, and then running the pyuic5 command, "from mplwidget import MplWidget" will appear in the design.py file which can be updated anytime without worry about overwritting. Then, create an mplwidget.py file with:

                # Imports
                from PyQt5 import QtWidgets
                from matplotlib.figure import Figure
                from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as Canvas
                import matplotlib
                
                # Ensure using PyQt5 backend
                matplotlib.use('QT5Agg')
                
                # Matplotlib canvas class to create figure
                class MplCanvas(Canvas):
                    def __init__(self):
                        self.fig = Figure()
                        self.ax = self.fig.add_subplot(111)
                        Canvas.__init__(self, self.fig)
                        Canvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
                        Canvas.updateGeometry(self)
                
                # Matplotlib widget
                class MplWidget(QtWidgets.QWidget):
                    def __init__(self, parent=None):
                        QtWidgets.QWidget.__init__(self, parent)   # Inherit from QWidget
                        self.canvas = MplCanvas()                  # Create canvas object
                        self.vbl = QtWidgets.QVBoxLayout()         # Set box for plotting
                        self.vbl.addWidget(self.canvas)
                        self.setLayout(self.vbl)
                

                然后,应用程序框架可以像我之前一样.运行并按下绘图按钮时,该图按预期显示.我想我基本上已经做了所有事情来手动提升"QWidget,只是错过了vbl的东西,但是每次编辑Qt Designer表单时,所有这些都会被覆盖.

                Then, the app framework can be as I had earlier. When run, and pushing the plot button, the figure appears as expected. I think I basically had done everything to "Promote" the QWidget by hand just missing the vbl stuff, but all that would be overwritten everytime the Qt Designer form is editted.

                app_framework.py:

                app_framework.py:

                from PyQt5.QtWidgets import QApplication, QMainWindow
                import design
                
                class MyApp(QMainWindow, design.Ui_mainWindow):
                   def __init(self):
                       super(self.__class__, self).__init__()
                       self.setupUi(self)
                       self.pushButton_plotData.clicked.connect(self.plot_data)
                
                    def plot_data(self):
                        x=range(0, 10)
                        y=range(0, 20, 2)
                        self.plotWidget.canvas.ax.plot(x, y)
                        self.plotWidget.canvas.draw()
                

                main.py:

                from PyQt5 import QtWidgets
                import sys
                
                # Local Module Imports
                import app_framework as af
                
                # Create GUI application
                app = QtWidgets.QApplication(sys.argv)
                form = af.MyApp()
                form.show()
                app.exec_()
                

                这篇关于使用 Qt Designer 表单和 PyQt5 在 QWidget 中绘制 matplotlib 图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在字符串中按下 QpushButton 时如何在 QlineEdit 中获 下一篇:如何从我的 Python 文件中更新 Qml 对象的属性?

                相关文章

                最新文章

              • <legend id='HDM4n'><style id='HDM4n'><dir id='HDM4n'><q id='HDM4n'></q></dir></style></legend>

                  <bdo id='HDM4n'></bdo><ul id='HDM4n'></ul>

                <small id='HDM4n'></small><noframes id='HDM4n'>

              • <i id='HDM4n'><tr id='HDM4n'><dt id='HDM4n'><q id='HDM4n'><span id='HDM4n'><b id='HDM4n'><form id='HDM4n'><ins id='HDM4n'></ins><ul id='HDM4n'></ul><sub id='HDM4n'></sub></form><legend id='HDM4n'></legend><bdo id='HDM4n'><pre id='HDM4n'><center id='HDM4n'></center></pre></bdo></b><th id='HDM4n'></th></span></q></dt></tr></i><div id='HDM4n'><tfoot id='HDM4n'></tfoot><dl id='HDM4n'><fieldset id='HDM4n'></fieldset></dl></div>

                    <tfoot id='HDM4n'></tfoot>