【发布时间】:2013-06-17 06:32:33
【问题描述】:
我正在尝试创建一个标签,我可以在其中写入程序的当前状态 - 例如:
“正在读取数据...”
“正在处理数据...”
“完成。”
如果文本到达标签的底部,那么它应该随着文本自动滚动,以确保它显示最新的消息(就像控制台窗口一样)。我已经摆弄标签和滚动区域一个多小时了......但无济于事。我尝试将我的标签放在一个滚动区域内(这似乎是这里的相关答案所建议的) - 此代码是从 Qt Designer 生成的:
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(218, 137)
self.frame = QtGui.QFrame(Dialog)
self.frame.setGeometry(QtCore.QRect(209, 399, 161, 111))
self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame.setFrameShadow(QtGui.QFrame.Raised)
self.frame.setObjectName(_fromUtf8("frame"))
self.scrollArea = QtGui.QScrollArea(Dialog)
self.scrollArea.setGeometry(QtCore.QRect(10, 10, 201, 121))
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
self.scrollAreaWidgetContents = QtGui.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 199, 119))
self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
self.label = QtGui.QLabel(self.scrollAreaWidgetContents)
self.label.setGeometry(QtCore.QRect(15, 10, 151, 101))
self.label.setWordWrap(True)
self.label.setObjectName(_fromUtf8("label"))
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
然后我的主 .pyw 文件包含:
import sys
from gui_test import *
class MyForm(QtGui.QDialog):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.label.setText("Warning: When passing a QString to the constructor or calling setText(), make sure to sanitize your input, as QLabel tries to guess whether it displays the text as plain text or as rich text, a subset of HTML 4 markup. You may want to call setTextFormat() explicitly, e.g. in case you expect the text to be in plain format but cannot control the text source (for instance when displaying data loaded from the Web).")
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
而且滚动区域根本不提供任何滚动条,即使不是所有的文本都被显示出来。如果我强制滚动条始终处于打开状态,它们会显示为灰色,表明它认为没有要滚动的文本。
如果有人可以在这里帮助我,我将不胜感激。
【问题讨论】:
标签: python pyqt pyqt4 qt-designer qscrollarea