python qt消息框出错

时间:2014-04-21 17:13:06

标签: python qt pyqt pyside messagebox

消息框保持'闪烁':s。它不会崩溃,但它会继续打开并重新打开。我怎么能解决这个问题?

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()

1 个答案:

答案 0 :(得分:0)

您的程序逻辑错误。在get_value_from_user()方法中,您有while Correct_details==False:行。这是引起问题的线。一旦用户输入了错误的登录详细信息,他们就无法再次输入,您只需从self.UsernameEditself.PasswordEdit反复读出相同的登录详细信息,当然还有继续为了不正确,所以循环运行永远和循环的每次迭代,你弹出消息框说它们是不正确的。

上面提到的循环需要在代码中的其他位置(可能在调用get_value_from_user的方法中),这允许用户重新输入他们的用户名和密码。 get_value_from_user()方法应该返回变量Correct_Details,以便调用方法可以决定是否再次向用户显示登录表单,或者继续。