Python 如何从PyQt linedit对象获取输入?

Python 如何从PyQt linedit对象获取输入?,python,qt,pyqt,pyqt4,qlineedit,Python,Qt,Pyqt,Pyqt4,Qlineedit,我想获取用户在lineedit对象中输入的信息: 并将其存储在此变量中: 以下是lineedit对象的代码片段: self.nameLe = QtGui.QLineEdit(ContactCreator) self.nameLe.setGeometry(QtCore.QRect(10, 60, 171, 20)) self.nameLe.setAutoFillBackground(False) self.nameLe.setObjectName(_fromUtf8("name")) 完整代码

我想获取用户在lineedit对象中输入的信息:

并将其存储在此变量中:

以下是lineedit对象的代码片段:

self.nameLe = QtGui.QLineEdit(ContactCreator)
self.nameLe.setGeometry(QtCore.QRect(10, 60, 171, 20))
self.nameLe.setAutoFillBackground(False)
self.nameLe.setObjectName(_fromUtf8("name"))
完整代码如下:

你在找这个吗

更新:

def addContact(self):
    self.name = str(self.nameLe.text())
    print self.name
我甚至不知道为什么到处都有globals和函数,在创建gui时调用self.name=strself.nameLe.text。那没什么用

编辑:完整的工作代码,我不在乎的mysql部分

# import sqlite3
from PyQt4 import QtCore, QtGui

# conn = sqlite3.connect('Contacts')
# c = conn.cursor()

# def tableCreate():
#     c.execute('CREATE TABLE ContactInfo(Name TEXT, Age INT, MobilePhone TEXT, Address TEXT)')

# name = ""
# age = int()
# mobilenum = ""
# Adr = ""

# def dataEntry():
#     c.execute('INSERT INTO CustomerInfo (Name, Age, MobilePhone, Address) VALUES (?,?,?,?)',
#         (Name, Age, MobilePhone, Adr))
#     conn.commit()



# GUI Code ------------------------------------------------------------

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_ContactCreator(object):
    def setupCreateContactUi(self, ContactCreator):
        ContactCreator.setObjectName(_fromUtf8("ContactCreator"))
        ContactCreator.resize(484, 165)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("CClogo.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        ContactCreator.setWindowIcon(icon)
        name = ""
        age = int()
        mobilenum = ""
        address = ""
        self.addcontactBut = QtGui.QPushButton(ContactCreator)
        self.addcontactBut.setGeometry(QtCore.QRect(190, 100, 101, 23))
        self.addcontactBut.setObjectName(_fromUtf8("addcontact"))
        self.addcontactBut.clicked.connect(self.addContact)

        self.nameLe = QtGui.QLineEdit(ContactCreator)
        self.nameLe.setGeometry(QtCore.QRect(10, 60, 171, 20))
        self.nameLe.setAutoFillBackground(False)
        self.nameLe.setObjectName(_fromUtf8("name"))

        self.ageLe = QtGui.QLineEdit(ContactCreator)
        self.ageLe.setGeometry(QtCore.QRect(190, 60, 41, 20))
        self.ageLe.setObjectName(_fromUtf8("age"))
        self.ageLe.setText("")

        self.mobphoLe = QtGui.QLineEdit(ContactCreator)
        self.mobphoLe.setGeometry(QtCore.QRect(240, 60, 113, 20))
        self.mobphoLe.setObjectName(_fromUtf8("mobilephone"))

        self.adrLe = QtGui.QLineEdit(ContactCreator)
        self.adrLe.setGeometry(QtCore.QRect(360, 60, 113, 20))
        self.adrLe.setObjectName(_fromUtf8("address"))

        self.label = QtGui.QLabel(ContactCreator)
        self.label.setGeometry(QtCore.QRect(90, 40, 31, 20))
        self.label.setObjectName(_fromUtf8("label"))

        self.label_2 = QtGui.QLabel(ContactCreator)
        self.label_2.setGeometry(QtCore.QRect(200, 40, 21, 20))
        self.label_2.setObjectName(_fromUtf8("label_2"))

        self.label_3 = QtGui.QLabel(ContactCreator)
        self.label_3.setGeometry(QtCore.QRect(260, 40, 81, 20))
        self.label_3.setObjectName(_fromUtf8("label_3"))

        self.label_4 = QtGui.QLabel(ContactCreator)
        self.label_4.setGeometry(QtCore.QRect(370, 40, 101, 20))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.retranslateUi(ContactCreator)
        QtCore.QMetaObject.connectSlotsByName(ContactCreator)

    def addContact(self):
        self.name = str(self.nameLe.text())
        print self.name

    def retranslateUi(self, ContactCreator):
        ContactCreator.setWindowTitle(_translate("ContactCreator", "ContactCreator", None))
        self.addcontactBut.setText(_translate("ContactCreator", "Add New Contact", None))
        self.label.setText(_translate("ContactCreator", "Name", None))
        self.label_2.setText(_translate("ContactCreator", "Age", None))
        self.label_3.setText(_translate("ContactCreator", "  Mobile Phone", None))
        self.label_4.setText(_translate("ContactCreator", "      Address", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    ContactCreator = QtGui.QWidget()
    ui = Ui_ContactCreator()
    ui.setupCreateContactUi(ContactCreator)
    ContactCreator.show()
    sys.exit(app.exec_())

那么我应该把addContact定义放在哪里呢?抱歉,我仍在学习中。我用工作代码更新了我的答案,该代码由您修改。如果您在查找答案时修改了该代码,那么您可以向上投票并将其标记为答案吗?是的,但因为我没有足够的声誉公开显示该答案。@b您不能向上投票,但您可以接受此答案单击答案左侧的复选标记
# import sqlite3
from PyQt4 import QtCore, QtGui

# conn = sqlite3.connect('Contacts')
# c = conn.cursor()

# def tableCreate():
#     c.execute('CREATE TABLE ContactInfo(Name TEXT, Age INT, MobilePhone TEXT, Address TEXT)')

# name = ""
# age = int()
# mobilenum = ""
# Adr = ""

# def dataEntry():
#     c.execute('INSERT INTO CustomerInfo (Name, Age, MobilePhone, Address) VALUES (?,?,?,?)',
#         (Name, Age, MobilePhone, Adr))
#     conn.commit()



# GUI Code ------------------------------------------------------------

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_ContactCreator(object):
    def setupCreateContactUi(self, ContactCreator):
        ContactCreator.setObjectName(_fromUtf8("ContactCreator"))
        ContactCreator.resize(484, 165)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("CClogo.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        ContactCreator.setWindowIcon(icon)
        name = ""
        age = int()
        mobilenum = ""
        address = ""
        self.addcontactBut = QtGui.QPushButton(ContactCreator)
        self.addcontactBut.setGeometry(QtCore.QRect(190, 100, 101, 23))
        self.addcontactBut.setObjectName(_fromUtf8("addcontact"))
        self.addcontactBut.clicked.connect(self.addContact)

        self.nameLe = QtGui.QLineEdit(ContactCreator)
        self.nameLe.setGeometry(QtCore.QRect(10, 60, 171, 20))
        self.nameLe.setAutoFillBackground(False)
        self.nameLe.setObjectName(_fromUtf8("name"))

        self.ageLe = QtGui.QLineEdit(ContactCreator)
        self.ageLe.setGeometry(QtCore.QRect(190, 60, 41, 20))
        self.ageLe.setObjectName(_fromUtf8("age"))
        self.ageLe.setText("")

        self.mobphoLe = QtGui.QLineEdit(ContactCreator)
        self.mobphoLe.setGeometry(QtCore.QRect(240, 60, 113, 20))
        self.mobphoLe.setObjectName(_fromUtf8("mobilephone"))

        self.adrLe = QtGui.QLineEdit(ContactCreator)
        self.adrLe.setGeometry(QtCore.QRect(360, 60, 113, 20))
        self.adrLe.setObjectName(_fromUtf8("address"))

        self.label = QtGui.QLabel(ContactCreator)
        self.label.setGeometry(QtCore.QRect(90, 40, 31, 20))
        self.label.setObjectName(_fromUtf8("label"))

        self.label_2 = QtGui.QLabel(ContactCreator)
        self.label_2.setGeometry(QtCore.QRect(200, 40, 21, 20))
        self.label_2.setObjectName(_fromUtf8("label_2"))

        self.label_3 = QtGui.QLabel(ContactCreator)
        self.label_3.setGeometry(QtCore.QRect(260, 40, 81, 20))
        self.label_3.setObjectName(_fromUtf8("label_3"))

        self.label_4 = QtGui.QLabel(ContactCreator)
        self.label_4.setGeometry(QtCore.QRect(370, 40, 101, 20))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.retranslateUi(ContactCreator)
        QtCore.QMetaObject.connectSlotsByName(ContactCreator)

    def addContact(self):
        self.name = str(self.nameLe.text())
        print self.name

    def retranslateUi(self, ContactCreator):
        ContactCreator.setWindowTitle(_translate("ContactCreator", "ContactCreator", None))
        self.addcontactBut.setText(_translate("ContactCreator", "Add New Contact", None))
        self.label.setText(_translate("ContactCreator", "Name", None))
        self.label_2.setText(_translate("ContactCreator", "Age", None))
        self.label_3.setText(_translate("ContactCreator", "  Mobile Phone", None))
        self.label_4.setText(_translate("ContactCreator", "      Address", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    ContactCreator = QtGui.QWidget()
    ui = Ui_ContactCreator()
    ui.setupCreateContactUi(ContactCreator)
    ContactCreator.show()
    sys.exit(app.exec_())