Tuesday, March 29, 2022

Draw Rectangle and Ellipse with PyQt6

This demo program shows how to draw rectangle and ellipse and place at specific location on the window, specify fill colors, set size and pen size.

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
import sys
from PyQt6.QtWidgets import QApplication,  QWidget,  QLabel
from PyQt6.QtGui import  QPainter, QColor, QPen
from PyQt6.QtCore import Qt

class Window(QWidget):

    def __init__(self):
        super(Window, self).__init__()

        self.initUI()

    def initUI(self):


        self.setGeometry(25, 45, 640, 300)
        self.setWindowTitle('Post 3')
        self.show()
        
 
    def paintEvent(self, e):

        qp = QPainter()
        qp.begin(self)
        self.drawRectangles(qp)
        qp.end()
           
    
    def drawRectangles(self, qp):

        color = QColor(0, 0, 0)
        color.setNamedColor('#d4d4d4')
        qp.setPen(color)

        qp.setBrush(QColor('#ffffff'))
        qp.drawRect(25, 25, 150, 30)

        pen = QPen()
        pen.setWidth(5)
        qp.setBrush(QColor('#00aa00'))
        qp.drawEllipse(25, 80, 300, 150)

        qp.setBrush(QColor('#d4d4d4'))
        qp.drawEllipse(40, 115, 270, 110)


        ii = 0
        while ii < 24:
            ii = ii + 1
            iii = (ii * 24) + 10
            qp.setBrush(QColor('#00aa00'))
            qp.drawRect(iii, 260, 22, 15)

 
def main():

    app = QApplication(sys.argv)
    ex = Window()
    sys.exit(app.exec())


if __name__ == '__main__':
    main()

No comments:

Post a Comment