来自 QLineEdit 的文本不显示
Text from QLineEdit not displaying
我正在从行编辑中获取用户输入并将其显示在 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()
您当前正在构造函数中读取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
我正在从行编辑中获取用户输入并将其显示在 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()
您当前正在构造函数中读取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