在pyqt5的QWidget中显示图

时间:2018-11-14 20:39:46

标签: canvas pyqt5 python-3.6

我一直在尝试编写一个带有窗口和按钮的小程序,最后将绘制图形,并希望在带有按钮的窗口上绘制一些信息的图表。

我已经在网上寻找了帮助,但到目前为止想出了以下代码:

 import sys
 import os.path

 from PyQt5 import QtCore, QtGui, QtWidgets
 PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, 
 QPushButton
 import numpy as np

  class visa_resultat(QWidget):
  def __init__(self, parent=None):
    super(visa_resultat, self).__init__(parent)     
    self.B_Knapp =  QPushButton('Back', self)
    self.B_Knapp.move(310, 350)
    self.B_Knapp.resize(280,40) 

    self.L_Knapp =  QPushButton('Ladda', self)
    self.L_Knapp.move(10, 350)
    self.L_Knapp.resize(280,40) 

 def start_visa_resultat(self):
    self.Window = visa_resultat(self)
    self.setWindowTitle("Kolla resultatet")
    self.setCentralWidget(self.Window)
    self.Window.B_Knapp.clicked.connect(self.startResultatWindow)
    self.Window.L_Knapp.clicked.connect(self.loadYT)
    self.show()

 def loadYT(self):
    t,y = self.file_open()
    jump_height, pltVrs = jumpTests.oneJump(y,t)
    jumpTests.plotOneJump(t,pltVrs)

start_visa_resultat窗口中,我想有一块画布,可以在其中加载数据并在上面显示图形。

感谢您阅读所有这些内容,也许会对我有所帮助

1 个答案:

答案 0 :(得分:0)

尝试一下:

import sys
import os.path

from PyQt5      import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton

import pyqtgraph as pg                                          # +++

import numpy as np

class CustomPlot(pg.PlotWidget):                                # +++
    def __init__(self):
        pg.PlotWidget.__init__(self)
        self.plot()
        self.setWindowTitle('pyqtgraph BarGraphItem')     
        # create list of floats
        y1 = np.linspace(0, 20, num=20)
        # create horizontal list
        x = np.arange(20)
        bg1 = pg.BarGraphItem(x=x, height=y1, width=0.6, brush='r')
        self.addItem(bg1)

class CustomPlot2(pg.PlotWidget):                                # +++
    def __init__(self):
        pg.PlotWidget.__init__(self)
        theTitle = "pyqtgraph plot"
        y = [2,4,6,8,10,12,14,16,18,20]
        x = range(0,10)
        # create plot
        self.plot(x, y, title=theTitle, pen='r')
        self.showGrid(x=True,y=True)


class VisaResultat(QWidget):
    def __init__(self, parent=None):
        super(VisaResultat, self).__init__(parent)     
        self.B_Knapp =  QPushButton('Back', self)
#        self.B_Knapp.move(310, 350)
#        self.B_Knapp.resize(280,40) 

        self.L_Knapp =  QPushButton('Ladda', self)
#        self.L_Knapp.move(10, 350)
#        self.L_Knapp.resize(280,40) 


#    def start_visa_resultat(self):
#        self.Window = visa_resultat(self)
        self.setWindowTitle("Kolla resultatet")
#        self.setCentralWidget(self.Window)
        self.B_Knapp.clicked.connect(self.startResultatWindow)
        self.L_Knapp.clicked.connect(self.loadYT)

# +++        
        layoutH = QtWidgets.QHBoxLayout()
        layoutH.addWidget(self.L_Knapp)
        layoutH.addWidget(self.B_Knapp)
        self.layout = QtWidgets.QVBoxLayout(self) 
        self.pgcustom  = CustomPlot()             
        self.layout.addLayout(layoutH)
        self.layout.addWidget(self.pgcustom)

    def loadYT(self):
#        t,y = self.file_open()
#        jump_height, pltVrs = jumpTests.oneJump(y,t)
#        jumpTests.plotOneJump(t,pltVrs)
        self.pgcustom2  = CustomPlot2()
        self.layout.addWidget(self.pgcustom2)

# +++        
    def startResultatWindow(self):
        print("def startResultatWindow(self):")


if __name__ == "__main__":
    app = QApplication(sys.argv) 
    w = VisaResultat() 
    w.show() 
    app.exec_()

enter image description here