【问题标题】:Text from QLineEdit not displaying来自 QLineEdit 的文本未显示
【发布时间】:2017-12-18 11:30:22
【问题描述】:

我正在从行编辑中获取用户输入并将其显示在 QMessageBox 上,但由于某种原因它不会显示。我想也许我根本没有从 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() 

【问题讨论】:

  • 您必须阅读插槽中的文本:def display(self): self.mytext = self.le.text() msg = QMessageBox.about(self, 'msg', '%s' % self.mytext) print(self.mytext) self.te.append(self.mytext) self.le.setText("")
  • 谢谢你,它成功了,嗯,我可以看到你已经拥有近 25k 名声,恭喜你。你应该写这个作为我可以投票给你的答案,只是说。 @eyllanesc
  • 我已经发布了。

标签: python pyqt pyqt4 qlineedit


【解决方案1】:

你当前是在构造函数中读取QLineEdit,此时QLineEdit是空的,你必须在slot里面做:

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

注意:使用 clear() 清理 QLineEdit

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    相关资源
    最近更新 更多