Not the perfect code but it is working fine login form PyQt4



  • import sys
    import os
    import mysql.connector
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    from PyQt4.QtSql import *
    
    class Form(QDialog):
        def __init__(self, parent=None):
            super(Form,self).__init__(parent)
    
            self.db = QSqlDatabase.addDatabase("QMYSQL")
            self.db.setHostName("localhost")
            self.db.setDatabaseName("user")
            self.db.setUserName("root")
            self.db.setPassword("admingelo")
            self.db.open()
    
            self.username = QLineEdit(self)
            self.QUserLabel = QLabel("USERNAME")
    
            self.password = QLineEdit(self)
            self.QPasswordLabel = QLabel("PASSWORD")
            self.password.setEchoMode(QLineEdit.Password)
    
            self.btn_Submit = QPushButton("LOGIN")
    
            layout = QFormLayout()
            layout.addRow(self.QUserLabel,self.username)
            layout.addRow(self.QPasswordLabel,self.password)
            layout.addRow(self.btn_Submit)
    
            self.setLayout(layout)
    
            self.connect(self.btn_Submit, SIGNAL("clicked()"),self.Submit_btn)
    
        def Submit_btn(self):
            USERNAME = self.username.text()
            PASSWORD = self.password.text()
    
            self.query = QSqlQuery("SELECT username,password FROM users")
            while (self.query.next()):
                username = self.query.value(0).toString()
                password = self.query.value(1).toString()
    
            if USERNAME == username and PASSWORD == password:
                print "Login!"
            else:
                print "failed"
    
            
    app = QApplication(sys.argv)
    form = Form()
    form.show()
    app.exec_()
    

    Edit: Original post here: https://forum.qt.io/topic/75347/how-to-create-a-login-form-in-python-how-i-can-check-if-username-and-password-is-present-in-the-database-this-is-my-code


  • Moderators

    @Gelo Thank you but it would be better if you post it into the original thread so that everything is connected. I will copy it into the original thread.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.