0

I have been trying to code a small program with windows and buttons that in the end will plot a graph and hopefully a chart with some info on a window with buttons on it.

I have looked all over the web for help, but so far came up with the code below:

 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)

Here in the window start_visa_resultat I would like to have a canvas where I can load data and show a graph on.

Thanks for reading all this and maybe help me

bcperth
  • 2,191
  • 1
  • 10
  • 16
viktor
  • 1
  • 1

1 Answers1

0

Try it:

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

S. Nick
  • 12,879
  • 8
  • 25
  • 33
  • Perfect thank you! I know it is off topic but you do not happen to know how to Show of a text in a window, now Im useing QPlainTextEdit, but then you can change the text when useing the program. – viktor Nov 15 '18 at 13:30
  • see https://stackoverflow.com/questions/50182532/how-to-correctly-get-unicode-text-input-from-qplaintextedit/50183542?noredirect=1#comment87384935_50183542 – S. Nick Nov 15 '18 at 13:56