绘图API: 绘制文本的案例
import sys
from PyQt5.QtCore import Qt, QRegExp, QDir
from PyQt5.QtGui import QIcon, QFont, QPalette, QPixmap, QIntValidator, QDoubleValidator, QRegExpValidator, QPainter, \
QColor
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QToolTip, QHBoxLayout, QMainWindow, QLabel, QVBoxLayout, \
QDialog, QLineEdit, QGridLayout, QFormLayout, QTextEdit, QRadioButton, QCheckBox, QSlider, QSpinBox, QMessageBox, \
QInputDialog, QFontDialog, QColorDialog, QFileDialog
'''
绘图API: 绘制文本的案例
1.文本
2.各种图形(直线,点,椭圆,弧形,扇形,多边形)
3.图像
QPainter
painter = QPainter()
painter.begin()
painter.drawText()
painter.end()
必须在paintEvent事件方法中绘制各种元素
'''
class drawDemo(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 设置定位和左上角坐标
self.setGeometry(300, 300, 400, 250)
# 设置窗口标题
self.setWindowTitle('在窗口上绘制文本 的演示')
# 设置窗口图标
# self.setWindowIcon(QIcon('../web.ico'))
self.text = "在窗口上绘制文本"
def paintEvent(self,event):
painter = QPainter(self)
painter.begin(self)
print("1111")
painter.setPen(QColor(150,44,15))
painter.setFont(QFont('SimDun',25))
painter.drawText(event.rect(),Qt.AlignCenter,self.text)
painter.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
# 设置应用图标
app.setWindowIcon(QIcon('../web.ico'))
w = drawDemo()
w.show()
sys.exit(app.exec_())