使用qt designer在pyqt中打印Textbox和LineEdit中的值

时间:2015-04-20 18:13:28

标签: python qt pyqt4 qtextedit

这是我的 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(最近一次调用最后一次):文件   &#34; C:\ cygwin64 \ home \ Actual_test_gui.py&#34;,第29行,在generate_report中       data_line = self.lineEdit.displayText()AttributeError:&#39; AppGui&#39;对象没有属性&#39; lineEdit&#39;

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

1 个答案:

答案 0 :(得分:1)

我认为您在方法.ui中的以下行中忘记了generate_report

此外,你不能打电话给textEdit.text(),我很害怕。

这是替代提案:

data_text = self.ui.textEdit.toPlainText()
data_line = self.ui.lineEdit.displayText()