PyQt:QLineEdit中的文本未显示

问题描述 投票:0回答:1

我从行编辑中获取用户输入并将其显示在QMessageBox上,但由于某种原因它不会显示。我想也许我根本没有抓住QLineEdit的输入,但是当我尝试在终端上打印它时(它仍然不会显示btw)终端向下滚动,认识到其中有新数据但是没有显示它。得到我说的话?

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() 
python pyqt4
1个回答
2
投票

您当前正在构造函数中读取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

© www.soinside.com 2019 - 2024. All rights reserved.