PyQtGraph打开自己的窗口,而不是主应用程序窗口

问题描述

我正在使用<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <Meta charset="utf-8"> <Meta name="viewport" content="width=device-width,initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> </head> <body> <form class="needs-validation" novalidate> <div class="form-row"> <div class="col-md-4 mb-3"> <label for="validationServer01">First name</label> <input type="text" class="form-control is-valid" id="validationServer01" placeholder="First name" value="Mark" required> <div class="valid-Feedback">Looks good!</div> <div class="invalid-Feedback d-none">Please enter a name</div> </div> </div> <div class="form-row"> <div class="col-md-4 mb-3"> <button class="btn btn-primary" type="submit">Submit form</button> </div> </div> </form> </body> </html>用Python构建应用程序,该应用程序需要在主应用程序窗口中显示带有按钮和主程序的图形。我正在使用PyQtGraph作图。

代码如下:

Qt Designer

问题

它总是在自己的窗口中打开。如何设置为在应用程序窗口的窗口中打开?

解决方法

首先,请勿修改QtDesigner生成的代码(有关更多信息,请阅读herehere),因此必须重新生成文件:pyuic5 design.ui -o design。 py -x`

考虑到上述情况,解决方案是通过布局将PlotWidget放置在“ labelx”内(另一种解决方案可以是提升小部件):

from PyQt5 import QtCore,QtGui,QtWidgets
import pyqtgraph as pg

from design import Ui_MainWindow


class MainWindow(QtWidgets.QMainWindow,Ui_MainWindow):
    def __init__(self,parent=None):
        super().__init__(parent)
        self.setupUi(self)
        self.pushButton_2.clicked.connect(self.lineEdit_13.clear)
        self.pushButton_2.clicked.connect(self.lineEdit_14.clear)
        self.pushButton_2.clicked.connect(self.lineEdit_15.clear)
        self.pushButton_4.clicked.connect(self.plotwid)

        lay = QtWidgets.QVBoxLayout(self.labelx)
        lay.setContentsMargins(0,0)
        self.plt = pg.plot()
        lay.addWidget(self.plt)

    def plotwid(self):

        title = "Savings Calculator"
        t = 8
        # y values to plot by line 1
        y = [1,5,6,8,11,14,13,18,30]

        # y values to plot by line 2
        y2 = [1,1,9,16,17,30]
        x = range(0,10)

        self.plt.showGrid(x=True,y=True)

        # adding legend
        self.plt.addLegend()

        # set properties of the label for y axis
        self.plt.setLabel("left","Amount of savings and income",units="y")

        # set properties of the label for x axis
        self.plt.setLabel("bottom","Months to save",units="s")

        # setting horizontal range
        self.plt.setXRange(0,12)

        # setting vertical range
        self.plt.setYRange(0,1000)

        # setting window title
        self.plt.setWindowTitle(title)

        # ploting line in green color
        line1 = self.plt.plot(
            x,y,pen="g",symbol="x",symbolPen="g",symbolBrush=0.2,name="green"
        )

        # ploting line2 with blue color
        line2 = self.plt.plot(
            x,y2,pen="b",symbol="o",symbolPen="b",name="blue"
        )


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

enter image description here