| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
- from PyQt5.QtGui import QPixmap, QPainter, QColor, QPen
- from PyQt5.QtCore import Qt
- from random import randint, choice
- class MainWindow(QMainWindow):
- def __init__(self):
- super().__init__()
- self.initUI()
- def initUI(self):
- self.label = QLabel()
- canvas = QPixmap(400, 300)
- canvas.fill(Qt.white)
- self.label.setPixmap(canvas)
- self.setCentralWidget(self.label)
- self.draw_something()
- def draw_something(self):
- colors = [
- "#ffd141",
- "#376f9f",
- "#0d1f2d",
- "#e9ebef",
- "#eb5160",
- ]
- painter = QPainter(self.label.pixmap())
- pen = QPen()
- pen.setWidth(3)
- pen.setColor(QColor("red"))
- painter.setPen(pen)
- for n in range(10000):
- pen.setColor(QColor(choice(colors)))
- painter.setPen(pen)
- painter.drawPoint(
- 200 + randint(-100, 100),
- 150 + randint(-100, 100),
- )
- painter.end()
- if __name__ == '__main__':
- import sys
- app = QApplication(sys.argv)
- window = MainWindow()
- window.show()
- sys.exit(app.exec_())
|