|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+import sys
|
|
|
2
|
+from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QLabel
|
|
|
3
|
+from PyQt5.QtGui import QPainter, QColor, QPen
|
|
|
4
|
+from PyQt5.QtGui import QIcon
|
|
|
5
|
+from PyQt5.QtCore import Qt
|
|
|
6
|
+import random
|
|
|
7
|
+
|
|
|
8
|
+class MyWindow(QMainWindow):
|
|
|
9
|
+
|
|
|
10
|
+ def __init__(self):
|
|
|
11
|
+ super().__init__()
|
|
|
12
|
+ self.title = 'PyQt rectangle colors - pythonspot.com'
|
|
|
13
|
+ self.left = 10
|
|
|
14
|
+ self.top = 10
|
|
|
15
|
+ self.width = 440
|
|
|
16
|
+ self.height = 280
|
|
|
17
|
+ self.initUI()
|
|
|
18
|
+
|
|
|
19
|
+ def initUI(self):
|
|
|
20
|
+ self.setWindowTitle(self.title)
|
|
|
21
|
+ self.setGeometry(self.left, self.top, self.width, self.height)
|
|
|
22
|
+
|
|
|
23
|
+ # Set window background color
|
|
|
24
|
+ self.setAutoFillBackground(True)
|
|
|
25
|
+ p = self.palette()
|
|
|
26
|
+ p.setColor(self.backgroundRole(), Qt.white)
|
|
|
27
|
+ self.setPalette(p)
|
|
|
28
|
+
|
|
|
29
|
+ # Add paint widget and paint
|
|
|
30
|
+ self.m = PaintWidget(self)
|
|
|
31
|
+ self.m.move(0,0)
|
|
|
32
|
+ self.m.resize(self.width,self.height)
|
|
|
33
|
+
|
|
|
34
|
+ self.show()
|
|
|
35
|
+
|
|
|
36
|
+class PaintWidget(QWidget):
|
|
|
37
|
+ def paintEvent(self, event):
|
|
|
38
|
+ qp = QPainter(self)
|
|
|
39
|
+
|
|
|
40
|
+ qp.setPen(Qt.black)
|
|
|
41
|
+ size = self.size()
|
|
|
42
|
+
|
|
|
43
|
+ # Colored rectangles
|
|
|
44
|
+ qp.setBrush(QColor(200, 0, 0))
|
|
|
45
|
+ qp.drawRect(0, 0, 100, 100)
|
|
|
46
|
+
|
|
|
47
|
+ qp.setBrush(QColor(0, 200, 0))
|
|
|
48
|
+ qp.drawRect(100, 0, 100, 100)
|
|
|
49
|
+
|
|
|
50
|
+ qp.setBrush(QColor(0, 0, 200))
|
|
|
51
|
+ qp.drawRect(200, 0, 100, 100)
|
|
|
52
|
+
|
|
|
53
|
+ # Color Effect
|
|
|
54
|
+ for i in range(0,100):
|
|
|
55
|
+ qp.setBrush(QColor(i*10, 0, 0))
|
|
|
56
|
+ qp.drawRect(10*i, 100, 10, 32)
|
|
|
57
|
+
|
|
|
58
|
+ qp.setBrush(QColor(i*10, i*10, 0))
|
|
|
59
|
+ qp.drawRect(10*i, 100+32, 10, 32)
|
|
|
60
|
+
|
|
|
61
|
+ qp.setBrush(QColor(i*2, i*10, i*1))
|
|
|
62
|
+ qp.drawRect(10*i, 100+64, 10, 32)
|
|
|
63
|
+
|
|
|
64
|
+if __name__ == '__main__':
|
|
|
65
|
+ app = QApplication(sys.argv)
|
|
|
66
|
+ ex = MyWindow()
|
|
|
67
|
+ sys.exit(app.exec_())
|