【问题标题】:How do I get the input from a PyQt linedit object?如何从 PyQt qlineedit 对象获取输入?
【发布时间】:2015-11-30 23:39:18
【问题描述】:

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

并将其存储在此变量中:

这是 lineedit 对象的 sn-p 代码:

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

完整代码在这里:http://pastebin.com/TGS9iNJH

【问题讨论】:

    标签: python qt pyqt pyqt4 qlineedit


    【解决方案1】:
    name = str(self.nameLe.text())
    

    你在找这个吗?

    更新:

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

    我什至不知道为什么你到处都有全局变量和函数内部函数,而你在创建 gui 时调用 self.name = str(self.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 定义放在哪里呢? (对不起,我还在学习。)
    • 我用你修改的工作代码更新了我的答案
    • 如果您正在寻找,那么您可以投票并标记为答案吗?
    • 我做到了,但是因为我没有足够的声誉来公开展示它。
    • @bmanner 你不能投票,但你可以接受这个答案(点击答案左侧的复选标记)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多