凌的博客

您现在的位置是: 首页 > 学无止境 > python > 

python

34. PyQt6 绘制点

2023-10-17 python 612
import sys, random
from PyQt6.QtWidgets import QApplication, QWidget
from PyQt6.QtGui import QGuiApplication, QPainter
from PyQt6.QtCore import Qt


class App(QWidget):

    def __init__(self):
        super().__init__()
        self.text = None
        self.setMinimumSize(50, 50)
        self.initUI()

    def initUI(self):
        self.setWindowTitle("PyQt6 绘制点")

        self.setGeometry(100, 100, 400, 300)
        self.center()

    def paintEvent(self, event):
        qp = QPainter()
        qp.begin(self)
        self.drawPoints(event, qp)
        qp.end()

    def drawPoints(self, event, qp):
        qp.setPen(Qt.GlobalColor.red)
        size = self.size()
        for i in range(1000):
            x = random.randint(1, size.width() - 1)
            y = random.randint(1, size.height() - 1)
            qp.drawPoint(x, y)

    def center(self):
        qr = self.frameGeometry()
        cp = QGuiApplication.primaryScreen().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    cls_app = App()
    cls_app.show()
    sys.exit(app.exec())

image.png

文章评论

0条评论