Search code examples
pythonqtpyqt4qtextedit

Print the values from Textbox and LineEdit in pyqt using qt designer


Here is my test_gui.py file. This is coverted from the .ui file generated by QT designer.

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

I am using another code to call this code and implement it using custom slots. Here it is: I am trying to print the text from the textbox and lineedit to the output. In the textBox we have the details that is entered by the user and lineedit will contain the filename. All I need to do is print the user detail and filename on screen. I get the following error:

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

Solution

  • I think you forgot .ui in the following lines in method generate_report.

    Besides, you can't call textEdit.text(), I'm afraid.

    Here a replacement proposal:

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