窗口无法正确显示qtpython

时间:2019-03-01 15:03:41

标签: pyqt5 pyqtgraph

当我点击一个按钮时,我试图在一个简单的窗口吃午餐。我复制的这个简单窗口来自:MultipleplotAxis.py

我的代码是:

from PyQt5 import QtCore, QtGui, QtWidgets
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(264, 248)
        MainWindow.setMinimumSize(QtCore.QSize(264, 248))
        MainWindow.setMaximumSize(QtCore.QSize(264, 248))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 264, 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.clicked['bool'].connect(self.lanzar_grafica_3_ejes)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))    
        self.pushButton.setText(_translate("MainWindow", "PushButton"))


   def lanzar_grafica_3_ejes(self):

        self.lanzar_a_canasta = lanzar()
        self.lanzar_a_canasta.setupUi(self)


class lanzar(object):

    ###########
    # COPY FROM PYQTGRAPH.ORG
    ###########

    def __init__(self):
        print('__init__ lanzar class')

    def setupUi(self, MainWindow):

        import time 

        pg.mkQApp()

        pw = pg.PlotWidget()
        pw.show()
        time.sleep(5)
        pw.setWindowTitle('pyqtgraph example: MultiplePlotAxes') 
        p1 = pw.plotItem
        p1.setLabels(left='axis 1')

        ## create a new ViewBox, link the right axis to its coordinate system
        p2 = pg.ViewBox()
        p1.showAxis('right')
        p1.scene().addItem(p2)
        p1.getAxis('right').linkToView(p2)
        p2.setXLink(p1)
        p1.getAxis('right').setLabel('axis2', color='#0000ff')

        ## create third ViewBox. 
        ## this time we need to create a new axis as well.
        p3 = pg.ViewBox()
        ax3 = pg.AxisItem('right')
        p1.layout.addItem(ax3, 2, 3)
        p1.scene().addItem(p3)
        ax3.linkToView(p3)
        p3.setXLink(p1)
        ax3.setZValue(-10000)
        ax3.setLabel('axis 3', color='#ff0000') 


        ## Handle view resizing 
        def updateViews():
            ## view has resized; update auxiliary views to match
            global p1, p2, p3
            p2.setGeometry(p1.vb.sceneBoundingRect())
            p3.setGeometry(p1.vb.sceneBoundingRect())

            ## need to re-update linked axes since this was called
            ## incorrectly while views had different shapes.
            ## (probably this should be handled in ViewBox.resizeEvent)
            p2.linkedViewChanged(p1.vb, p2.XAxis)
            p3.linkedViewChanged(p1.vb, p3.XAxis)

        updateViews()
        p1.vb.sigResized.connect(updateViews)
        p1.plot([10,20,40,80,40,20],[1,2,4,8,16,32])
        p2.addItem(pg.PlotCurveItem([10,20,40,80,40,20],[12,13,14,15,16,17], pen='b'))
        p3.addItem(pg.PlotCurveItem([10,20,40,80,40,20],[32,16,8,4,2,1], pen='r'))


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_())

当我按下按钮时,我想显示的窗口没有完全出现,我的意思是:

这是我要显示的窗口: good image,但是当我单击按钮时,仅显示框架,内部没有任何内容。我还编写了一个计时器,因为如果没有,则该窗口会立即消失。

我想我以错误的方式调用了课程。

有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

尝试一下:

from PyQt5 import QtCore, QtGui, QtWidgets
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
#import time 


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(264, 248)
        MainWindow.setMinimumSize(QtCore.QSize(264, 248))
#        MainWindow.setMaximumSize(QtCore.QSize(264, 248))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 264, 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.clicked['bool'].connect(self.lanzar_grafica_3_ejes)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))    
        self.pushButton.setText(_translate("MainWindow", "PushButton"))

#    def lanzar_grafica_3_ejes(self):   ##(MainWindow): #
#        self.lanzar_a_canasta = Lanzar()
#        self.lanzar_a_canasta.setupUi() #(self)


class Lanzar(QtWidgets.QWidget):    #(object):

    ###########
    # COPY FROM PYQTGRAPH.ORG
    ###########

    def __init__(self):
        super().__init__()
        print('__init__ Lanzar class')


    def setupUi(self, MainWindow):

        pg.mkQApp()

        self.pw = pg.PlotWidget()
#        self.pw.show()
#        time.sleep(5)
        self.pw.setWindowTitle('pyqtgraph example: MultiplePlotAxes') 
        self.p1 = self.pw.plotItem
        self.p1.setLabels(left='axis 1')

        ## create a new ViewBox, link the right axis to its coordinate system
        self.p2 = pg.ViewBox()
        self.p1.showAxis('right')
        self.p1.scene().addItem(self.p2)
        self.p1.getAxis('right').linkToView(self.p2)
        self.p2.setXLink(self.p1)
        self.p1.getAxis('right').setLabel('axis2', color='#0000ff')

        ## create third ViewBox. 
        ## this time we need to create a new axis as well.
        self.p3 = pg.ViewBox()
        ax3 = pg.AxisItem('right')
        self.p1.layout.addItem(ax3, 2, 3)
        self.p1.scene().addItem(self.p3)
        ax3.linkToView(self.p3)
        self.p3.setXLink(self.p1)
        ax3.setZValue(-10000)
        ax3.setLabel('axis 3', color='#ff0000') 


        ## Handle view resizing 
        def updateViews():
            ## view has resized; update auxiliary views to match
#            global p1, p2, p3
            self.p2.setGeometry(self.p1.vb.sceneBoundingRect())
            self.p3.setGeometry(self.p1.vb.sceneBoundingRect())

            ## need to re-update linked axes since this was called
            ## incorrectly while views had different shapes.
            ## (probably this should be handled in ViewBox.resizeEvent)
            self.p2.linkedViewChanged(self.p1.vb, self.p2.XAxis)
            self.p3.linkedViewChanged(self.p1.vb, self.p3.XAxis)

        updateViews()
        self.p1.vb.sigResized.connect(updateViews)
        self.p1.plot([10,20,40,80,40,20],[1,2,4,8,16,32])
        self.p2.addItem(pg.PlotCurveItem([10,20,40,80,40,20],[12,13,14,15,16,17], pen='b'))
        self.p3.addItem(pg.PlotCurveItem([10,20,40,80,40,20],[32,16,8,4,2,1], pen='r'))


class Window(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):                                         
        super().__init__()

        self.setupUi(self)

        self.pushButton.clicked.connect(self.lanzar_grafica_3_ejes)


    def lanzar_grafica_3_ejes(self):    
        self.lanzar_a_canasta = Lanzar()
        self.lanzar_a_canasta.setupUi(self.lanzar_a_canasta)    

        self.gridLayout.addWidget(self.lanzar_a_canasta.pw, 1, 0, 1, 5)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Window()
    w.show()
    w.resize(630, 550)
    sys.exit(app.exec_())

enter image description here