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

问题描述 投票:0回答:1

在我的代码中,使用一个固定尺寸的QLabel。一开始,我 赋文 "你好,Python "到 该QLablel. 现在我想 更改QLablel的文字 到 "海,欢迎来到Python"。

如何 减小QLabel的字体大小,并显示完整的文本。 与该特定的区域大小(没有任何切断隐藏)?

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_())
python pyqt pyqt5 qlabel
1个回答
2
投票

QFontMetrics类提供了字体度量信息。https:/doc.qt.ioqt-5qfontmetrics.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_())

enter image description here


0
投票

使用 .setFont()

例如..:

self.labl.setFont(QFont('Arial', 10))
© www.soinside.com 2019 - 2024. All rights reserved.