如何增加/减少 PyQt5 QLabel 中的字体大小(固定大小)?

How to Increase /decrease the font size In PyQt5 QLabel (Fixed Size)?

在我的代码中,使用一个固定 dimension/size 的 QLabel。一开始,我将text"Hello,Python"分配给that QLablel。现在我想 将 QLablel 的文本更改为 "Hai, Welcome to Python"。

如何减小 QLabel 的字体大小,并显示带有特定 area/size 的全文(没有任何截断/隐藏/隐藏)?

import sys
from PyQt5.QtWidgets import *


class Labelexample(QWidget):
    def __init__(self):
        super().__init__()
        self.labl = QLabel("Hello,Python",self)
        self.tbox = QLineEdit(self)
        self.labl.setFixedSize(150,30)
        self.tbox.setFixedSize(200,30)
        self.labl.move(50,50)
        self.tbox.move(140,50)
        self.labl.setText("Hai, welcome to Python")
        # self.labl.adjustsize()

app = QApplication(sys.argv)
mywin = Labelexample()
mywin.show()
sys.exit(app.exec_())

使用.setFont()

例如:

self.labl.setFont(QFont('Arial', 10))

QFontMetrics class 提供字体规格信息。 https://doc.qt.io/qt-5/qfontmetrics.html#QFontMetrics

试一试:

import sys
from PyQt5.Qt import *


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

        text = "Hello,Python"
        self.labl = QLabel(text, self)
#        self.labl.setFixedSize(150,30)
        self.labl.setFixedWidth(150)                            # +++
        self.labl.setMinimumHeight(30)                          # +++

        self.labl.move(50,50)
        self.labl.setStyleSheet("background-color: yellow;")

        self.tbox = QLineEdit(self)
        self.tbox.setFixedSize(200,30)
#        self.tbox.move(140,50)
        self.tbox.move(200,50)
        self.tbox.returnPressed.connect(self.return_pressed)    # +++ 

        self.pointSize = 20                                     # 20
        self.fontD = self.font()
        self.fontD.setPointSize(self.pointSize)
        self.metrics = QFontMetrics(self.fontD)
        self.width = self.metrics.width(text)
        self.height = self.metrics.height()
        self.labl.setFixedWidth(self.width)
        self.labl.setFont(self.fontD)        

#        text = "Hai, welcome to Python"
#        self.labl.setText(text)                                 #         v             v
        self.labl.adjustSize()                                   # - adjustsize  + adjustSize

    def return_pressed(self):                                    # +++
        if not self.tbox.text(): 
            self.labl.setText('')
            self.labl.setFont(self.fontD)
            self.labl.adjustSize()
            return
        width = self.metrics.width(self.tbox.text())
        height = self.metrics.height()

        if not width: 
            width = 1

        koef = self.width / width
        pointSize = int(self.pointSize * koef)
        font = self.font()
        font.setPointSize(pointSize)
        self.labl.setFont(font)
        height = self.metrics.height()
        self.labl.setText(self.tbox.text())
        self.labl.adjustSize()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mywin = Labelexample()
    mywin.resize(450, 415)
    mywin.show()
    sys.exit(app.exec_())