1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| from PyQt5.QtWidgets import (QMainWindow,QApplication,QWidget,QSpinBox,
QLabel,QDoubleSpinBox,QCheckBox,QGraphicsScene,
QGraphicsView,QVBoxLayout,QGridLayout,QPushButton)
from PyQt5.QtGui import (QPen, QPainter,QImage,QFont,QBrush)
from PyQt5 import QtCore
import sys
import numpy as np
import operator
class fenPrincipale(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
fenetre_widget = QWidget()
self.pen=QPen()
self.pen.setColor(QtCore.Qt.black)
self.pen2=QPen()
self.pen2.setColor(QtCore.Qt.red)
self.pen2.setWidth(2)
self.bouton=QPushButton("Save")
self.label_grand_rayon=QLabel()
self.label_grand_rayon.setText("Grand rayon:")
self.label_grand_rayon.setFixedWidth(100)
self.label_petit_rayon=QLabel()
self.label_petit_rayon.setText("Petit rayon:")
self.label_petit_rayon.setFixedWidth(100)
self.label_angle=QLabel()
self.label_angle.setText("Angle:")
self.label_angle.setFixedWidth(20)
self.angle=QDoubleSpinBox()
self.angle.setRange(0, 90)
self.angle.setSingleStep(1)
self.angle.setValue(0)
self.grand_rayon=QDoubleSpinBox()
self.grand_rayon.setRange(2,999)
self.grand_rayon.setSingleStep(1)
self.grand_rayon.setValue(200)
self.petit_rayon=QDoubleSpinBox()
self.petit_rayon.setRange(2,999)
self.petit_rayon.setSingleStep(1)
self.petit_rayon.setValue(150)
self.scene=QGraphicsScene()
self.view =QGraphicsView(self.scene)
LayoutV=QVBoxLayout()
LayoutGrid=QGridLayout()
LayoutV.addLayout(LayoutGrid)
LayoutGrid.addWidget(self.grand_rayon,0,1)
LayoutGrid.addWidget(self.label_grand_rayon,0,0)
LayoutGrid.addWidget(self.petit_rayon,1,1)
LayoutGrid.addWidget(self.label_petit_rayon,1,0)
LayoutGrid.addWidget(self.label_angle,1,2)
LayoutGrid.addWidget(self.angle,1,3)
LayoutGrid.addWidget(self.bouton,0,3)
LayoutV.addWidget(self.view)
fenetre_widget.setLayout(LayoutV)
self.setCentralWidget(fenetre_widget)
#deg=180*rad/pi
self.grand_rayon.valueChanged.connect(self.dessin_cercle)
self.petit_rayon.valueChanged.connect(self.dessin_cercle)
self.angle.valueChanged.connect(self.variation_angle)
self.bouton.clicked.connect(self.save_image)
self.resize(600,600)
self.dessin_cercle()
self.variation_angle()
def dessin_cercle(self):
self.scene.clear()
self.nbpoints=720
self.theta = np.linspace(0, 360, self.nbpoints)
self.x = np.cos(self.theta*np.pi/180)
self.y = np.sin(self.theta*np.pi/180)
self.coefficient=self.grand_rayon.value()
for i in range(0, self.nbpoints-1):
self.scene.addLine(self.x[i]*self.coefficient,self.y[i]*self.coefficient,self.x[i+1]*self.coefficient,self.y[i+1]*self.coefficient,self.pen)
self.coefficient=self.petit_rayon.value()
for i in range(0, self.nbpoints-1):
self.scene.addLine(self.x[i]*self.coefficient,self.y[i]*self.coefficient,self.x[i+1]*self.coefficient,self.y[i+1]*self.coefficient,self.pen)
def variation_angle(self):
try:
print(self.trait1)
self.scene.removeItem(self.trait1)
except:
print("pas de ligne1")
print("5")
try:
print(self.trait2)
self.scene.removeItem(self.trait2)
except:
print("pas de ligne2")
print("6")
try:
print(self.trait3)
self.scene.removeItem(self.trait3)
except:
print("pas de ligne3")
try:
self.scene.removeItem(self.label_erreur)
except:
print("pas de légende!")
self.x_petit_rayon=(np.cos(self.angle.value()*np.pi/180))*self.petit_rayon.value()
self.h_petit_rayon=(np.sin(self.angle.value()*np.pi/180))*self.petit_rayon.value()
self.angle_eq_grand_rayon=np.arcsin((self.h_petit_rayon)/(self.grand_rayon.value()))*180/np.pi
self.x_eq_grand_rayon=np.cos(self.angle_eq_grand_rayon*np.pi/180)*self.grand_rayon.value()
self.mesure_vrai=self.grand_rayon.value()-self.petit_rayon.value()
self.mesure_mesuree=self.x_eq_grand_rayon-self.x_petit_rayon
self.pourcentage_erreur=(((self.x_eq_grand_rayon-self.x_petit_rayon)/(self.mesure_vrai))-1)*100
self.trait1=self.scene.addLine(self.x_petit_rayon,self.h_petit_rayon,0,0,self.pen)
self.trait2=self.scene.addLine(self.x_eq_grand_rayon,self.h_petit_rayon,0,0,self.pen)
self.trait3=self.scene.addLine(self.x_petit_rayon,self.h_petit_rayon,self.x_eq_grand_rayon,self.h_petit_rayon,self.pen2)
self.label_erreur=self.scene.addSimpleText (("Erreur (en %): " + str(round(self.pourcentage_erreur,2))), QFont('Norasi', float(14)))
self.label_erreur.setBrush(QBrush(QtCore.Qt.red))
self.label_erreur.setY(-self.grand_rayon.value())
self.label_erreur.setX(self.grand_rayon.value()/2)
def save_image(self):
outputimg = QPixmap(500, 500)
painter = QPainter(outputimg)
targetrect = QRectF(0, 0, 500, 500)
sourcerect = QRect(0, 0, 500, 500)
self.view.render(painter, targetrect, sourcerect)
outputimg.save("bibi.png", "PNG")
painter.end()
def main(args):
appli=QApplication(args)
fenetre=fenPrincipale()
fenetre.show()
r=appli.exec_()
return r
if __name__=="__main__":
main(sys.argv) |
Partager