美文网首页
PyQt5 Drawing points(绘制点) 学习

PyQt5 Drawing points(绘制点) 学习

作者: _Mirage | 来源:发表于2020-04-05 11:27 被阅读0次

    A point is the most simple graphics object that can be drawn. It is a small spot on the window.

    代码:

    # coding='utf-8'
    
    from PyQt5.QtWidgets import QApplication, QWidget
    from PyQt5.QtGui import QPainter
    from PyQt5.QtCore import Qt
    import sys, random
    
    
    class Gui(QWidget):
        def __init__(self):
            super(Gui, self).__init__()
            self.start()
    
        def start(self):
            self.setGeometry(300, 300, 300, 190)
            self.setWindowTitle('绘制点')
            self.show()
    
        # 触发绘图的slot
        # Each time we resize the window, a paint event is generated. 
        def paintEvent(self, event) -> None:
            # QPainter实例对象负责绘制所有低等级的绘图功能
            qp = QPainter()
            # 所有的绘图功能都得从QPainter实例对象的begin方法开始,\
            #     一直到它的end方法结束.
            # begin的参数:begin(self, QPaintDevice) -> bool
            #     注意begin比end多一个参数: 将图形绘制到哪里
            qp.begin(self)
            # 具体的绘制过程被我们放到了我们自定义的函数里面
            self.draw_points(event, qp)
            qp.end()
    
        def draw_points(self, event, qp):
            # 设置绘图颜色为红色
            qp.setPen(Qt.red)
            # 得到主窗体的大小(宽,长)的二元组
            size = self.size()
            # 绘制1000个点
            for i in range(1000):
                # 在主窗体范围内随机区域绘制点
                x = random.randint(1, size.width()-1)
                y = random.randint(1, size.height()-1)
                qp.drawPoint(x, y)
    
    
    app = QApplication(sys.argv)
    gui = Gui()
    sys.exit(app.exec_())
    
    
    运行结果: image.png

    相关文章

      网友评论

          本文标题:PyQt5 Drawing points(绘制点) 学习

          本文链接:https://www.haomeiwen.com/subject/nwnyphtx.html