简体   繁体   中英

How can I make my QLabel scroll with the written text

Im making a calculator with pyqt5 but when the calculator QLabel overflow I need it to scroll with the last digit on the QLabel

在此处输入图片说明

One option is to use a read-only QLineEdit instead of a QLabel . For example

from PyQt5 import QtWidgets, QtGui
from PyQt5.QtCore import Qt


class Window(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.label = QtWidgets.QLineEdit()
        # mimic QLabel by making self.label read-only and removing the frame and background color
        self.label.setReadOnly(True)
        self.label.setStyleSheet("background-color:#00000000; font-size: 20px; border:0px")
        self.label.setAlignment(Qt.AlignCenter)

        self.text_edit = QtWidgets.QLineEdit(self)
        self.text_edit.setPlaceholderText('type something')

        self.vlayout = QtWidgets.QVBoxLayout(self)
        self.vlayout.addWidget(self.label)
        self.vlayout.addWidget(self.text_edit)

        self.text_edit.textChanged.connect(self.label.setText)


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    window = Window()
    window.show()
    app.exec()

Screenshots:

截图 1 截图 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM