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_())

***代码运行效果如上: ***在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43008143/article/details/107802069
今日推荐