使用qt设计在pyqt中打印文本框和LineEdit中的值

2024-05-11 08:07:44 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的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_())

Tags: textmargintestselfuistyleguilabel