#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat May 12 18:06:42 2018 @author: saltz """ import sys from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QAction, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QSpacerItem, QSizePolicy, QPushButton, QMessageBox from PyQt5.QtGui import QIcon from PyQt5.QtCore import QSize from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.figure import Figure import matplotlib.pyplot as plt import random import numpy as np from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar class MainWindow(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.title = 'test' self.left = 20 self.top = 20 self.width = 1000 self.height = 1000 self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) self.statusBar().showMessage('Ready') mainMenu = self.menuBar() mainMenu.setNativeMenuBar(False) fileMenu = mainMenu.addMenu('File') helpMenu = mainMenu.addMenu('Help') exitButton = QAction(QIcon('exit24.png'), 'Exit', self) exitButton.setShortcut('Ctrl+Q') exitButton.setStatusTip('Exit application') exitButton.triggered.connect(self.close) fileMenu.addAction(exitButton) widget = QWidget(self) self.setCentralWidget(widget) vlay = QVBoxLayout(widget) hlay = QHBoxLayout() vlay.addLayout(hlay) self.nameLabel = QLabel('Name:', self) self.line = QLineEdit(self) self.nameLabel2 = QLabel('Result', self) hlay.addWidget(self.nameLabel) hlay.addWidget(self.line) hlay.addWidget(self.nameLabel2) hlay.addItem(QSpacerItem(1000, 10, QSizePolicy.Expanding)) pybutton = QPushButton('Click me', self) pybutton.clicked.connect(self.clickMethod) pybuttoncos = QPushButton('Courbe cos', self) pybuttoncos.clicked.connect(self.clickPlotcos) pybuttonsin = QPushButton('Courbe SIN', self) pybuttonsin.clicked.connect(self.plotsin) pybuttonalea = QPushButton('Courbe alea', self) pybuttonalea.clicked.connect(self.plotaleatoire) hlay2 = QHBoxLayout() hlay2.addWidget(pybutton) hlay2.addWidget(pybuttoncos) hlay2.addWidget(pybuttonsin) hlay2.addWidget(pybuttonalea) hlay2.addItem(QSpacerItem(1000, 10, QSizePolicy.Expanding)) vlay.addLayout(hlay2) m = WidgetPlot(self) # appelle la classe WidgetPlot cad affiche les widgets qui ensuite affichera les 3 canvas vlay.addWidget(m) def clickPlotcos(self): print ("l'évènement sur le bouton Courbe cos réagit c'est la courbe qui ne se dessine pas") x = np.linspace(0, 2*np.pi, 30) y = np.cos(x) axcos = self.figure.add_subplot(111) # create an axis axcos.plot(x, y) self.canvas1.draw() # canvas1 pas détecté dans self donc cela ne marche pas axcos.set_title('Cosinus') def plotsin(self): print ("la courbe n'est pas dessinée sur le bon canevas") msg = QMessageBox() msg.setIcon(QMessageBox.Information) msg.setText("This is a message box") msg.setInformativeText("This is additional information") msg.setWindowTitle("MessageBox demo") msg.setDetailedText("The details are as follows:") msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel) msg.exec_() # c'est .exec_() qui fait s'afficher la message box à l'écran x = np.linspace(-np.pi, np.pi, 201) plt.plot(x, np.sin(x)) plt.xlabel('Angle [rad]') plt.ylabel('sin(x)') plt.axis('tight') plt.show() def plotaleatoire(self): print("hmm tjs le meme probleme") data = [random.random() for i in range(250)] ax = self.figure.add_subplot(111) ax.plot(data, 'r-', linewidth = 0.5) ax.set_title('PyQt Matplotlib Example') self.draw() def clickMethod(self): print('Clicked Pyqt button.') if self.line.text() == '': self.statusBar().showMessage('Not a Number') else: print('Number: {}'.format(float(self.line.text())*2)) self.statusBar().showMessage('Introduction of a number') self.nameLabel2.setText(str(float(self.line.text())*2)) class WidgetPlot(QWidget): def __init__(self, *args, **kwargs): QWidget.__init__(self, *args, **kwargs) self.setLayout(QVBoxLayout()) self.canvas = PlotCanvas(self, width=10, height=8) self.toolbar = NavigationToolbar(self.canvas, self) self.canvas1 = PlotCanvas(self, width=10, height=8) self.toolbar1 = NavigationToolbar(self.canvas1, self) self.canvas2 = PlotCanvas(self, width=10, height=8) self.toolbar2 = NavigationToolbar(self.canvas2, self) self.layout().addWidget(self.toolbar) self.layout().addWidget(self.canvas) self.layout().addWidget(self.toolbar1) self.layout().addWidget(self.canvas1) self.layout().addWidget(self.toolbar2) self.layout().addWidget(self.canvas2) class PlotCanvas(FigureCanvas): def __init__(self, parent=None, width=10, height=8, dpi=100): fig = Figure(figsize=(width, height), dpi=dpi) FigureCanvas.__init__(self, fig) self.setParent(parent) FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding) FigureCanvas.updateGeometry(self) #self.plotaleatoire() if __name__ == "__main__": app = QApplication(sys.argv) mainWin = MainWindow() mainWin.show() sys.exit( app.exec_())