import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter, QBrush, QColor, QFont
from PyQt5.QtWidgets import QWidget, QApplication, QLabel
class CircleLabel(QLabel):
def init(self, parent=None):
super().init(parent=parent)
# 设置标签大小和对齐方式
self.setFixedSize(100, 100)
self.setAlignment(Qt.AlignCenter)
def paintEvent(self, event):
painter = QPainter(self)
brush = QBrush(QColor(255, 0, 0)) # 红色画刷
painter.setBrush(brush)
painter.drawEllipse(10, 10, 80, 80) # 绘制圆形
# 调用父类的 paintEvent() 方法进行标签文字的绘制
super().paintEvent(event)
if name == ‘main’:
app = QApplication(sys.argv)
label = CircleLabel()
label.setText(‘Hello, world!’)
label.show()
sys.exit(app.exec_())