PyQt:来自QLineEdit的文本不显示
我从行编辑中抓取用户输入并将其显示在QMessageBox上,但由于某种原因它不会显示。我想也许我没有从QLineEdit获取输入,但是当我尝试在终端上打印它(它仍然不会显示任何btw)终端向下滚动时,认识到有新的数据,但没有显示它。知道我在说什么?PyQt:来自QLineEdit的文本不显示
import os import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
def main():
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
# create objects
label = QLabel(self.tr("enter the data "))
self.le = QLineEdit()
self.te = QTextEdit()
# layout
layout = QVBoxLayout(self)
layout.addWidget(label)
layout.addWidget(self.le)
layout.addWidget(self.te)
self.setLayout(layout)
# create connection
self.mytext = str(self.le.text())
self.connect(self.le, SIGNAL("returnPressed(void)"),
self.display)
def display(self):
QApplication.instance().processEvents()
msg = QMessageBox.about(self, 'msg', '%s' % self.mytext)
print(self.mytext)
self.te.append(self.mytext)
self.le.setText("")
if __name__ == "__main__":
main()
回答:
您正在阅读在构造函数中QLineEdit的,在那一刻的QLineEdit的是空的,你必须这样做在插槽:
def display(self): mytext = self.le.text()
msg = QMessageBox.about(self, 'msg', '%s' % mytext)
self.te.append(mytext)
self.le.clear()
注意:使用clear()来清洁QLineEdit
以上是 PyQt:来自QLineEdit的文本不显示 的全部内容, 来源链接: utcz.com/qa/262964.html