PyQt5学习之在窗口上绘制文本

在窗口上绘制文本(2020年8月4号)

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(400,300)
        self.text = "自律学习PyQt5"
    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_())

***代码运行效果如上: ***PyQt5学习之在窗口上绘制文本_第1张图片

你可能感兴趣的:(PyQt5的学习,pyqt)