我怎样才能让我的 QLabel 滚动文字
How can I make my QLabel scroll with the written text
我正在用 pyqt5 制作一个计算器,但是当计算器 QLabel 溢出时,我需要它滚动 QLabel 上的最后一位数字
一个选项是使用只读 QLineEdit
而不是 QLabel
。例如
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()
截图:
我正在用 pyqt5 制作一个计算器,但是当计算器 QLabel 溢出时,我需要它滚动 QLabel 上的最后一位数字
一个选项是使用只读 QLineEdit
而不是 QLabel
。例如
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()
截图: