简体   繁体   English

来自 QLineEdit 的文本不显示

[英]Text from QLineEdit not displaying

I am grabbing the user input from the line edit and displaying it on the QMessageBox but it won't show for some reason.我从行编辑中获取用户输入并将其显示在 QMessageBox 上,但由于某种原因它不会显示。 I thought maybe I wasn't grabbing the input from QLineEdit at all but when I tried printing it on the terminal (it still wouldn't show there btw) the terminal scrolled down, recognizing that there is new data in it but just not displaying it.我想也许我根本没有从 QLineEdit 获取输入但是当我尝试在终端上打印它时(它仍然不会显示在那里)终端向下滚动,认识到其中有新数据但只是不显示它。 Get what I am saying?明白我在说什么吗?

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() 

You are currently reading the QLineEdit in the constructor, and at that moment the QLineEdit is empty, you must do it in the slot:您当前正在构造函数中读取 QLineEdit,此时 QLineEdit 为空,您必须在插槽中执行它:

def display(self):
    mytext = self.le.text()
    msg = QMessageBox.about(self, 'msg', '%s' % mytext)
    self.te.append(mytext)
    self.le.clear()

Note: use clear() to clean the QLineEdit注意:使用 clear() 来清理 QLineEdit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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