美文网首页
PyQt5 Painting --> Drawing text(

PyQt5 Painting --> Drawing text(

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

    Painting in PyQt5
    PyQt5 painting system is able to render vector graphics, images, and outline font-based text. Painting is needed in applications when we want to change or enhance an existing widget, or if we are creating a custom widget from scratch. To do the drawing, we use the painting API provided by the PyQt5 toolkit.

    QPainter
    QPainter performs low-level painting on widgets and other paint devices. It can draw everything from simple lines to complex shapes.
    The paintEvent method

    The painting is done within the paintEvent() method. The painting code is placed between the begin() and end() methods of the QPainter object. It performs low-level painting on widgets and other paint devices.

    Drawing text(绘制文本)
    We begin with drawing some Unicode text on the client area of a window.

    代码:

    # coding='utf-8'
    
    from PyQt5.QtWidgets import QApplication, QWidget
    from PyQt5.QtGui import QPainter, QColor, QFont
    from PyQt5.QtCore import Qt
    import sys
    
    
    class Gui(QWidget):
        def __init__(self):
            super(Gui, self).__init__()
            self.start()
    
        def start(self):
            # 创建待绘制到窗体的文本
            self.text = 'Лев Николаевич Толстой\nАнна Каренина'
    
            self.setGeometry(300, 300, 400, 170)
            self.setWindowTitle('画文本')
            self.show()
    
        # 这个slot会在绘图时自动触发,绘图是在这个paintEvent里面完成的
        # 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_text(event, qp)
            qp.end()
    
        def draw_text(self, event, qp):
            # qp.setPen的参数:
            """
            setPen(self, Union[QColor, Qt.GlobalColor, QGradient])
            setPen(self, Union[QPen, QColor, Qt.GlobalColor, QGradient])
            setPen(self, Qt.PenStyle)
            """
            # 我们这里设置了绘制用的颜色
            qp.setPen(QColor(168, 34, 3))
            # 这个设置了绘制用的字体
            qp.setFont(QFont('Decorative', 10))
            # drawText构造函数:
            """
            drawText(self, Union[QPointF, QPoint], str)
            drawText(self, QRectF, int, str) -> QRectF
            drawText(self, QRect, int, str) -> QRect
            drawText(self, QRectF, str, option: QTextOption = QTextOption())
            drawText(self, QPoint, str)
            drawText(self, int, int, int, int, int, str) -> QRect
            drawText(self, int, int, str)
            """
            # Qt.AlignCenter使得我们可以将文本朝两个维度(垂直/水平)对齐
            # event.rect()是一个我们要在哪里绘制更新的矩形区域
            qp.drawText(event.rect(), Qt.AlignCenter, self.text)
    
    
    
    app = QApplication(sys.argv)
    gui = Gui()
    sys.exit(app.exec_())
    
    
    运行结果: image.png

    相关文章

      网友评论

          本文标题:PyQt5 Painting --> Drawing text(

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