如何更改文本的笔划粗细?

How to change the stroke thickness of a text?

我想更改文字的粗细。

在css中有一个属性这个

-webkit-text-stroke-width

PyQt5有模拟吗

将字体更改为更细的字体不是一种选择,因为我使用的是一种没有粗体斜体版本等的独特字体。

import sys
from PyQt5.QtWidgets import (QRadioButton, QHBoxLayout, QButtonGroup, 
    QApplication, QGraphicsScene,QGraphicsView, QGraphicsLinearLayout, QGraphicsWidget, QWidget, QLabel)
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import QSize, QPoint,Qt
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QPainter


class Window(QWidget):
    def __init__(self):
        super().__init__()

        self.resize(800, 800)
        label = QLabel(self)
        label.setText('<div>&#xe202;</div>')

        font_id = QFontDatabase.addApplicationFont(url)
        if font_id == -1:
            print('not fond')



        font = QFont("my-font",18)



        label.setStyleSheet('''font-size: 80pt; font-family: my-font;''')



if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())

你可以试试font-weight 属性,值100-900有不同的粗细。 QFont.setWeight().

有一个等效的方法
class Template(QWidget):

    def __init__(self):
        super().__init__()
        grid = QGridLayout(self)
        grid.addWidget(QLabel('Hello World'), 0, 0, Qt.AlignHCenter)
        for i in range(1, 10):
            lbl = QLabel(f'({i * 100}) Hello World')
            lbl.setStyleSheet(f'font-weight: {i * 100}')
            grid.addWidget(lbl, i, 0)
        self.setStyleSheet('''
        QLabel {
            font-size: 24pt;
            font-family: Helvetica Neue;
        }''')

看起来像这样:

尽管这不适用于所有字体。您也可以将 QLabel 子类化并重新实现 paintEvent 以绘制 window 颜色的文本轮廓。

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class ThinLabel(QLabel):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def paintEvent(self, event):
        qp = QPainter(self)
        qp.setRenderHint(QPainter.Antialiasing)
        path = QPainterPath()
        path.addText(event.rect().bottomLeft(), self.font(), self.text())
        qp.setPen(QPen(self.palette().color(QPalette.Window), 2))
        qp.setBrush(self.palette().text())
        qp.drawPath(path)


class Template(QWidget):

    def __init__(self):
        super().__init__()
        grid = QGridLayout(self)
        grid.addWidget(QLabel('Hello World'), 0, 0)
        grid.addWidget(ThinLabel('Hello World'), 1, 0)
        self.setStyleSheet('''
        QLabel {
            font-size: 80pt;
            font-family: Helvetica Neue;
        }''')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    gui = Template()
    gui.show()
    sys.exit(app.exec_())