在使用Python QT開發界面的時候,會出現從登陸界面到主界面的一個切換問題。如下所示,用戶輸入用戶名和密碼后,如果用戶名和密碼匹配,則會跳轉到主界面,此時登陸界面消失。
首先需要定義自己需要的對話框,請參考:http://blog.csdn.net/lovelyaiq/article/details/55050580。在代碼加入對話中加入對話框退出時的返回值,例如:accept(),reject()。
def save_button_click(self): global Current_user Current_user=str(self.lineEdit_name.text()) self.passwd_input=str(self.lineEdit_wd.text()) passwd_check=False username_check=False if not Current_user.endswith("@163.com"): QtGui.QMessageBox.warning(self, "Error", u'請檢查輸入的用戶名格式,需要以"@163.com"結尾', buttons=QtGui.QMessageBox.Ok, defaultButton=QtGui.QMessageBox.Ok) else: username_check=True if self.passwd != self.passwd_input and username_check: QtGui.QMessageBox.warning(self, "Error", u'您輸入的密碼有誤 !!!', buttons=QtGui.QMessageBox.Ok, defaultButton=QtGui.QMessageBox.Ok) else: passwd_check=True if username_check and passwd_check: self.accept() 當用戶點擊登陸按鈕時,軟件會檢測用戶名和密碼是否匹配,如果匹配,則返回accept(),在主程序中需要接受到這個返回值。if dialog.exec_()==QtGui.QDialog.Accepted: ui = Ui() ui.show()其中dialog.exec_()表示等待登陸界面退出,如果登陸界面退出時的返回值是QtGui.QDialog.Accepted,那么將會啟動主界面。注:在兩個界面切換的過程中,使用到了登陸界面退出時的返回值,根據這個返回值判斷是否啟動主界面。關于exec_()的解釋,文檔的說明為:
Shows the dialog as a modal dialog, blocking until the user closes it. The function returns a DialogCode result.
新聞熱點
疑難解答