سؤال

The messagebox keeps 'flashing' :s. It doesn't crash, but it just keeps on opening and re-opening. How could I fix this problem?

self.retranslateUi(Login)
    QtCore.QObject.connect(self.ExitButton, QtCore.SIGNAL(_fromUtf8("clicked()")), Login.close)
    QtCore.QObject.connect(self.LoginButton, QtCore.SIGNAL("clicked()"),self.get_value_from_user)
    QtCore.QMetaObject.connectSlotsByName(Login)
    Login.setTabOrder(self.LoginButton, self.UsernameEdit)
    Login.setTabOrder(self.UsernameEdit, self.PasswordEdit)
    Login.setTabOrder(self.PasswordEdit, self.ExitButton)

def get_value_from_user(self):
    Correct_details = False
    while Correct_details==False:
        inputusername=self.UsernameEdit.text()
        inputpassword=self.PasswordEdit.text()
        cur.execute("SELECT password FROM tblStaff WHERE username='"+inputusername+"'")
        password=str(cur.fetchone())
        password=password[2:-3]
        cur.execute("SELECT firstname FROM tblStaff WHERE username='"+inputusername+"'")
        FirstName=str(cur.fetchone())
        FirstName=FirstName[2:-3]
        cur.execute("SELECT surname FROM tblStaff WHERE username='"+inputusername+"'")
        Surname=str(cur.fetchone())
        Surname=Surname[2:-3]
        if str(password) == str(inputpassword):
            self.msgBox1 = QMessageBox(QMessageBox.Information,'Successful', "Succesfully Logged in")
            self.msgBox1.show()
            Correct_details = True
        else:
            self.msgBox2 = QMessageBox(QMessageBox.Information, 'Warning', "The Username or Password you have entered is incorrect")
            self.msgBox2.show()
هل كانت مفيدة؟

المحلول

Your program logic is wrong. Inside the get_value_from_user() method, you have the line while Correct_details==False:. This is the line that is causing the problem. Once the user has entered their incorrect login details, they don't get the option to enter them again, you just repeatedly read out the same login details from self.UsernameEdit and self.PasswordEdit and of course the continue to not be correct, and so the loop runs forever and each iteration of the loop, you pop up the message box saying they are incorrect.

The loop mentioned above needs to be somewhere else in your code (possibly in the method that calls get_value_from_user), that allows the user to re-enter their username and password. The get_value_from_user() method should probably return the variable Correct_Details so that the calling method can decide whether to present the user with the login form again, or proceed.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top