This demo program shows how to draw a polygon, draw lines at certain angle, draw text using html.
The output:
The code:
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 | import sys from PyQt6.QtWidgets import QApplication, QWidget from PyQt6.QtGui import QPainter, QColor, QPen, QPolygonF, QTextDocument from PyQt6.QtCore import QPointF, QRect, QRectF import math class Window(QWidget): def __init__(self): super(Window, self).__init__() self.initUI() def initUI(self): self.polygon = self.createPoly(8,150,0) self.setGeometry(25, 45, 400, 325) #self.resize(600, 600) self.setWindowTitle('Post 16') self.show() def createPoly(self, n, r, s): polygon = QPolygonF() polygon.append(QPointF(9, 12)) polygon.append(QPointF(-9, 12)) polygon.append(QPointF(0, -100)) return polygon def paintEvent(self, e): qp = QPainter() qp.begin(self) self.drawRectangles(qp) qp.end() def drawRectangles(self, qp): pen = QPen() pen.setWidth(20) color = QColor(0, 0, 0) color.setNamedColor('#00aa00') qp.setPen(color) qp.setBrush(QColor('#ffffff')) qp.drawRect(25, 25, 350, 275) qp.translate(200,200) qp.drawPolygon(self.polygon) qp.setBrush(QColor('#00aa00')) qp.drawEllipse(-5, -5, 10, 10) qp.drawLine(125, 0, 110, 0) qp.drawText(130, 0, "100") ii=1 while ii < 100: qp.rotate(-1.8) if (ii % 10) == 0: qp.drawLine(125, 0, 110, 0) qp.drawText(130, 0, str(100 - ii)) elif (ii % 5) == 0: qp.drawLine(120, 0, 110, 0) else: qp.drawLine(120, 0, 115, 0) ii = ii + 1 qp.rotate(-1.8) qp.drawLine(125, 0, 110, 0) qp.rotate(180) qp.drawText(-135, 0, "0") #qp.drawText(-8, 30, "50") qp.translate(-35,10) document = QTextDocument() rect2 = QRectF(0,0,100,100) document.setTextWidth(100) document.setHtml("<font size = '10' color='red'>50</font><font size = '5' color='black'>db</font>") document.drawContents( qp, rect2) def main(): app = QApplication(sys.argv) ex = Window() sys.exit(app.exec()) if __name__ == '__main__': main() |
No comments:
Post a Comment