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

Posted

技术标签:

【中文标题】PyQtGraph 打开它自己的窗口,而不是主应用程序窗口【英文标题】:PyQtGraph opens its own window, not main app window 【发布时间】:2021-01-22 11:54:03 【问题描述】:

我正在使用Qt Designer 在 Python 中构建一个应用程序,它需要在主应用程序窗口中显示一个带有按钮和主程序的图形。我正在使用 PyQtGraph 进行绘图。

代码如下:

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

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(668, 458)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.labelx = QtWidgets.QLabel(self.centralwidget)
        self.labelx.setGeometry(QtCore.QRect(50, 20, 581, 231))
        self.labelx.setAutoFillBackground(True)
        self.labelx.setFrameShape(QtWidgets.QFrame.Panel)
        self.labelx.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.labelx.setLineWidth(3)
        self.labelx.setMidLineWidth(3)
        self.labelx.setText("")
        self.labelx.setScaledContents(True)
        self.labelx.setObjectName("labelx")
        self.label_15 = QtWidgets.QLabel(self.centralwidget)
        self.label_15.setGeometry(QtCore.QRect(350, 280, 91, 16))
        self.label_15.setObjectName("label_15")
        self.lineEdit_13 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_13.setGeometry(QtCore.QRect(230, 280, 113, 20))
        self.lineEdit_13.setObjectName("lineEdit_13")
        self.lineEdit_14 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_14.setGeometry(QtCore.QRect(230, 310, 113, 20))
        self.lineEdit_14.setText("")
        self.lineEdit_14.setObjectName("lineEdit_14")
        self.label_16 = QtWidgets.QLabel(self.centralwidget)
        self.label_16.setGeometry(QtCore.QRect(350, 310, 91, 16))
        self.label_16.setObjectName("label_16")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(10, 390, 111, 23))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(120, 390, 111, 23))
        self.pushButton_2.setObjectName("pushButton_2")
        self.lineEdit_15 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_15.setGeometry(QtCore.QRect(440, 280, 113, 20))
        self.lineEdit_15.setText("")
        self.lineEdit_15.setObjectName("lineEdit_15")
        self.label_17 = QtWidgets.QLabel(self.centralwidget)
        self.label_17.setGeometry(QtCore.QRect(560, 280, 91, 16))
        self.label_17.setObjectName("label_17")
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(550, 390, 111, 23))
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_4.setGeometry(QtCore.QRect(230, 340, 111, 23))
        self.pushButton_4.setObjectName("pushButton_4")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 668, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        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)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label_15.setText(_translate("MainWindow", "Total Expenses"))
        self.label_16.setText(_translate("MainWindow", "Total Income"))
        self.pushButton.setText(_translate("MainWindow", "Add Expenses"))
        self.pushButton_2.setText(_translate("MainWindow", "Cleasr All"))
        self.label_17.setText(_translate("MainWindow", "Total Savings"))
        self.pushButton_3.setText(_translate("MainWindow", "Exit"))
        self.pushButton_4.setText(_translate("MainWindow", "Make Graph"))

    def plotwid(self):

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

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

        # create plot window object
        plt = pg.plot()
        # showing x and y grids
        plt.showGrid(x=True, y=True)

        # adding legend
        plt.addLegend()

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

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

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

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

        # setting window title
        plt.setWindowTitle(title)

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

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

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

问题

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

【问题讨论】:

【参考方案1】:

首先,不要修改QtDesigner生成的代码(更多信息请阅读here和here)所以你必须重新生成文件:pyuic5 design.ui -o design.py -x`

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

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, 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, 6, 11, 14, 13, 18, 30]

        # y values to plot by line 2
        y2 = [1, 1, 5, 8, 9, 11, 16, 17, 14, 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", symbolBrush=0.2, name="blue"
        )


if __name__ == "__main__":
    import sys

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

【讨论】:

非常感谢!我正在学习如何使用 QTDesigner 和 pyqtgraph。它非常强大,我需要一些时间来了解它们是如何协同工作的。你的信息帮助了我。非常感谢.....另外,我为什么不修改代码?我还能如何对按钮等进行编程,并制作我想要构建的程序?

以上是关于PyQtGraph 打开它自己的窗口,而不是主应用程序窗口的主要内容,如果未能解决你的问题,请参考以下文章

pyqtgraph绘图如何使用pyqtgraph

关闭时从另一个应用程序打开 Spotify 主窗口

分组窗口在窗口列表中显示脚本名称而不是标题

PyQtGraph 不会调整大小

打开新窗口而不关注它[重复]

强制 Firefox 打开新标签而不是新窗口