其他分享
首页 > 其他分享> > PyQt5基础学习-QPainter(绘画组件) 1. painter.setPen(设置画笔的颜色) 2. painter.setFont(设置画笔的字体) 3. painter.drawText

PyQt5基础学习-QPainter(绘画组件) 1. painter.setPen(设置画笔的颜色) 2. painter.setFont(设置画笔的字体) 3. painter.drawText

作者:互联网

设置画笔的颜色和字体,然后进行书写文本

"""
绘图API: 绘制文本

1.文本
2.各种图形(直线, 点, 椭圆, 弧, 扇形, 多边形等)
3.图像

QPainter
painter = QPainter()

painter.begin()

painter.drawText()

painter.end()

必须在painterEvent事件方法中绘制各种元素
"""
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPainter, QColor, QFont
from PyQt5.QtCore import Qt

class DrawText(QWidget):
    def __init__(self):
        super(DrawText, self).__init__()
        self.setWindowTitle("在窗口上绘制文本")
        self.resize(600, 200)
        self.text = "Pyhon从菜鸟到高手"
    def paintEvent(self, event):
        painter = QPainter(self)
        painter.begin(self)
        #设置画笔颜色
        painter.setPen(QColor(150, 43, 5))
        #设置字体大小
        painter.setFont(QFont('SimSun', 25))
        #设置要书写的内容
        painter.drawText(event.rect(), Qt.AlignCenter, self.text)
        painter.end()



if __name__ == "__main__":
    app = QApplication(sys.argv)

    main = DrawText()
    main.show()

    sys.exit(app.exec_())

 

标签:__,画笔,self,PyQt5,painter,设置,import,QPainter
来源: https://www.cnblogs.com/hyq-lst/p/15858358.html