pythonpyqt5qlabelautomatic-updates

automatically insert '...' at the end when text is too large to display in one line in pyqt5


I want to insert '...' string at the end of label if inner text is too large to display in one line. Now. It displays 'this is my shcool tea' I want to change 'tea' to '...' automatically. is there any function in python?

  class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()

        lable = QLabel(self)
        lable.setText('this is my school teacher')

    def enterEvent(self,event):

        pass
    def mousePressEvent(self,event):
        self.calc()
    def calc(self):

        pass

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())

Important is to deal with several font.


Solution

  • Well, you could set a limit for the string length, like limit=10 Then you could check if the string exceeds the limit

    if len(text)>limit:
        text=text[:limit]+"..."
    

    Then you could set the label in __init__:

    label.setText(text)