adityatandon

Python GUI QT5

Nov 4th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. import sys
  2.  
  3. from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePolicy, QMessageBox, QWidget, QPushButton
  4. from PyQt5.QtGui import QIcon
  5.  
  6.  
  7. from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
  8. from matplotlib.figure import Figure
  9. import matplotlib.pyplot as plt
  10.  
  11. import random
  12.  
  13. class App(QMainWindow):
  14.  
  15. def __init__(self):
  16. super().__init__()
  17. self.left = 10
  18. self.top = 10
  19. self.title = 'PyQt5 matplotlib example - pythonspot.com'
  20. self.width = 640
  21. self.height = 400
  22. self.initUI()
  23.  
  24. def initUI(self):
  25. self.setWindowTitle(self.title)
  26. self.setGeometry(self.left, self.top, self.width, self.height)
  27.  
  28. m = PlotCanvas(self, width=5, height=4)
  29. m.move(0,0)
  30.  
  31. button = QPushButton('PyQt5 button', self)
  32. button.setToolTip('This s an example button')
  33. button.move(500,0)
  34. button.resize(140,100)
  35.  
  36. self.show()
  37.  
  38.  
  39. class PlotCanvas(FigureCanvas):
  40.  
  41. def __init__(self, parent=None, width=5, height=4, dpi=100):
  42. fig = Figure(figsize=(width, height), dpi=dpi)
  43. self.axes = fig.add_subplot(111)
  44.  
  45. FigureCanvas.__init__(self, fig)
  46. self.setParent(parent)
  47.  
  48. FigureCanvas.setSizePolicy(self,
  49. QSizePolicy.Expanding,
  50. QSizePolicy.Expanding)
  51. FigureCanvas.updateGeometry(self)
  52. self.plot()
  53.  
  54.  
  55. def plot(self):
  56. data = [random.random() for i in range(25)]
  57. ax = self.figure.add_subplot(111)
  58. ax.plot(data, 'r-')
  59. ax.set_title('PyQt Matplotlib Example')
  60. self.draw()
  61.  
  62. if __name__ == '__main__':
  63. app = QApplication(sys.argv)
  64. ex = App()
  65. sys.exit(app.exec_())
Add Comment
Please, Sign In to add comment