繁体   English   中英

使用Qt Designer在pyqt中打印Textbox和LineEdit中的值

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

这是我的test_gui.py文件。 这是由QT设计器生成的.ui文件.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将包含文件名。 我需要做的就是在屏幕上打印用户详细信息和文件名。 我收到以下错误:

追溯(最近一次通话):文件“ C:\\ cygwin64 \\ home \\ Actual_test_gui.py”,第29行,在generate_report data_line = self.lineEdit.displayText()中AttributeError:'AppGui'对象没有属性'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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM