2
votes

In my code, use one QLabel with fixed dimension/size. At beginning, I assign text "Hello,Python" to that QLablel. Now I want to change the text of the QLablel to "Hai, Welcome to Python".

How to decrease the font size of the QLabel, and display full text with that particular area/size (without any cut off / hidde /hidden) ?

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_())
2

2 Answers

2
votes

The QFontMetrics class provides font metrics information. https://doc.qt.io/qt-5/qfontmetrics.html#QFontMetrics

Try it:

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
votes

use .setFont()

eg.:

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