当文本太大而无法在 pyqt5 的一行中显示时,在末尾自动插入 '...'

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

如果内部文本太大而无法在一行中显示,我想在标签末尾插入“...”字符串。 现在。它显示 'this is my shcool tea' 我想自动将 'tea' 更改为 '...'。 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_())

重要的是要处理几种字体。

嗯,你可以为字符串长度设置一个限制,比如 limit=10 然后你可以检查字符串是否超过限制

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

然后你可以在__init__中设置标签:

label.setText(text)