使用 qt designer 在 pyqt 中打印来自文本框和 LineEdit 的值
Print the values from Textbox and LineEdit in pyqt using qt designer
这是我的 test_gui.py 文件。这是从 QT 设计器生成的 .ui
文件中隐藏的。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test_gui.ui'
#
# Created: Mon Apr 20 13:49:36 2015
# by: PyQt4 UI code generator 4.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_test_gui(object):
def setupUi(self, test_gui):
test_gui.setObjectName(_fromUtf8("test_gui"))
test_gui.resize(400, 300)
self.textEdit = QtGui.QTextEdit(test_gui)
self.textEdit.setGeometry(QtCore.QRect(20, 40, 171, 101))
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.lineEdit = QtGui.QLineEdit(test_gui)
self.lineEdit.setGeometry(QtCore.QRect(80, 160, 113, 31))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.label = QtGui.QLabel(test_gui)
self.label.setGeometry(QtCore.QRect(10, 160, 81, 31))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(test_gui)
self.label_2.setGeometry(QtCore.QRect(20, 10, 81, 31))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.pushButton = QtGui.QPushButton(test_gui)
self.pushButton.setGeometry(QtCore.QRect(20, 230, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.retranslateUi(test_gui)
QtCore.QMetaObject.connectSlotsByName(test_gui)
def retranslateUi(self, test_gui):
test_gui.setWindowTitle(_translate("test_gui", "Dialog", None))
self.textEdit.setHtml(_translate("test_gui", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:10pt;\">Enter the following info:</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:10pt;\">Name:</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:10pt;\">Employee no:</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:10pt;\">Position:</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:10pt;\">Start Date:</span></p></body></html>", None))
self.label.setText(_translate("test_gui", "<html><head/><body><p><span style=\" font-size:12pt;\">Filename:</span></p></body></html>", None))
self.label_2.setText(_translate("test_gui", "<html><head/><body><p><span style=\" font-size:12pt;\">Person Info:</span></p><p><br/></p></body></html>", None))
self.pushButton.setText(_translate("test_gui", "Submit", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
test_gui = QtGui.QDialog()
ui = Ui_test_gui()
ui.setupUi(test_gui)
test_gui.show()
sys.exit(app.exec_())
我正在使用另一个代码来调用此代码并使用自定义插槽实现它。在这里:我正在尝试将文本框和 lineedit 中的文本打印到输出。在 textBox
中,我们有用户输入的详细信息,lineedit 将包含文件名。我需要做的就是在屏幕上打印用户详细信息和文件名。我收到以下错误:
Traceback (most recent call last): File
"C:\cygwin64\home\Actual_test_gui.py", line 29, in generate_report
data_line = self.lineEdit.displayText() AttributeError: 'AppGui' object has no attribute 'lineEdit'
import sys, os, shutil, glob
from PyQt4 import QtCore, QtGui
from test_gui import Ui_test_gui
from __builtin__ import str, int
from subprocess import Popen
class AppGui(QtGui.QDialog,Ui_test_gui):
def __init__(self):
QtGui.QDialog.__init__(self)
self.ui = Ui_test_gui()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.generate_report)
def generate_report(self):
data_text = self.textEdit.text().ascii()
data_line = self.lineEdit.displayText()
print data_line
print data_text
app = QtGui.QApplication(sys.argv)
window = AppGui()
ui = Ui_test_gui()
window.show()
sys.exit(app.exec_())
我想你在方法 generate_report
.
的以下几行中忘记了 .ui
此外,你不能打电话给textEdit.text()
,恐怕。
这里有一个替换建议:
data_text = self.ui.textEdit.toPlainText()
data_line = self.ui.lineEdit.displayText()
这是我的 test_gui.py 文件。这是从 QT 设计器生成的 .ui
文件中隐藏的。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test_gui.ui'
#
# Created: Mon Apr 20 13:49:36 2015
# by: PyQt4 UI code generator 4.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_test_gui(object):
def setupUi(self, test_gui):
test_gui.setObjectName(_fromUtf8("test_gui"))
test_gui.resize(400, 300)
self.textEdit = QtGui.QTextEdit(test_gui)
self.textEdit.setGeometry(QtCore.QRect(20, 40, 171, 101))
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.lineEdit = QtGui.QLineEdit(test_gui)
self.lineEdit.setGeometry(QtCore.QRect(80, 160, 113, 31))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.label = QtGui.QLabel(test_gui)
self.label.setGeometry(QtCore.QRect(10, 160, 81, 31))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(test_gui)
self.label_2.setGeometry(QtCore.QRect(20, 10, 81, 31))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.pushButton = QtGui.QPushButton(test_gui)
self.pushButton.setGeometry(QtCore.QRect(20, 230, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.retranslateUi(test_gui)
QtCore.QMetaObject.connectSlotsByName(test_gui)
def retranslateUi(self, test_gui):
test_gui.setWindowTitle(_translate("test_gui", "Dialog", None))
self.textEdit.setHtml(_translate("test_gui", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:10pt;\">Enter the following info:</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:10pt;\">Name:</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:10pt;\">Employee no:</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:10pt;\">Position:</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:10pt;\">Start Date:</span></p></body></html>", None))
self.label.setText(_translate("test_gui", "<html><head/><body><p><span style=\" font-size:12pt;\">Filename:</span></p></body></html>", None))
self.label_2.setText(_translate("test_gui", "<html><head/><body><p><span style=\" font-size:12pt;\">Person Info:</span></p><p><br/></p></body></html>", None))
self.pushButton.setText(_translate("test_gui", "Submit", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
test_gui = QtGui.QDialog()
ui = Ui_test_gui()
ui.setupUi(test_gui)
test_gui.show()
sys.exit(app.exec_())
我正在使用另一个代码来调用此代码并使用自定义插槽实现它。在这里:我正在尝试将文本框和 lineedit 中的文本打印到输出。在 textBox
中,我们有用户输入的详细信息,lineedit 将包含文件名。我需要做的就是在屏幕上打印用户详细信息和文件名。我收到以下错误:
Traceback (most recent call last): File "C:\cygwin64\home\Actual_test_gui.py", line 29, in generate_report data_line = self.lineEdit.displayText() AttributeError: 'AppGui' object has no attribute 'lineEdit'
import sys, os, shutil, glob
from PyQt4 import QtCore, QtGui
from test_gui import Ui_test_gui
from __builtin__ import str, int
from subprocess import Popen
class AppGui(QtGui.QDialog,Ui_test_gui):
def __init__(self):
QtGui.QDialog.__init__(self)
self.ui = Ui_test_gui()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.generate_report)
def generate_report(self):
data_text = self.textEdit.text().ascii()
data_line = self.lineEdit.displayText()
print data_line
print data_text
app = QtGui.QApplication(sys.argv)
window = AppGui()
ui = Ui_test_gui()
window.show()
sys.exit(app.exec_())
我想你在方法 generate_report
.
.ui
此外,你不能打电话给textEdit.text()
,恐怕。
这里有一个替换建议:
data_text = self.ui.textEdit.toPlainText()
data_line = self.ui.lineEdit.displayText()