pyqt draw

Solutions on MaxInterview for pyqt draw by the best coders in the world

showing results for - "pyqt draw"
Agustín
29 Sep 2018
1#!/usr/bin/python
2
3"""
4ZetCode PyQt5 tutorial
5
6In the example, we draw randomly 1000 red points
7on the window.
8
9Author: Jan Bodnar
10Website: zetcode.com
11"""
12
13from PyQt5.QtWidgets import QWidget, QApplication
14from PyQt5.QtGui import QPainter
15from PyQt5.QtCore import Qt
16import sys, random
17
18
19class Example(QWidget):
20
21    def __init__(self):
22        super().__init__()
23
24        self.initUI()
25
26    def initUI(self):
27        self.setGeometry(300, 300, 300, 190)
28        self.setWindowTitle('Points')
29        self.show()
30
31    def paintEvent(self, e):
32        qp = QPainter()
33        qp.begin(self)
34        self.drawPoints(qp)
35        qp.end()
36
37    def drawPoints(self, qp):
38        qp.setPen(Qt.red)
39        size = self.size()
40
41        if size.height() <= 1 or size.height() <= 1:
42            return
43
44        for i in range(1000):
45            x = random.randint(1, size.width() - 1)
46            y = random.randint(1, size.height() - 1)
47            qp.drawPoint(x, y)
48
49
50def main():
51    app = QApplication(sys.argv)
52    ex = Example()
53    sys.exit(app.exec_())
54
55
56if __name__ == '__main__':
57    main()
58