2016-08-12 16 views
1

我創建了一個登錄和註冊頁面,使用PyQt鏈接到其他GUI。大多數代碼可以工作,但是當我嘗試註冊一個新用戶時,它會給我一個AttributeError:'Ui_Login'對象沒有屬性'GetTable'。 GetTable在用MySQl創建的數據庫的代碼中定義,它被調用到Login類和Signup類中。 請在新的python和編程一般。對不起,如果這個問題似乎愚蠢。但我已閱讀了很多關於以前問過的問題,我似乎無法理解它在說什麼。感謝AttributeError:'Ui_Login'對象沒有屬性'GetTable'

# -*- coding: utf-8 -*- 

# Form implementation generated from reading ui file 'Login.ui' 
# 
# Created by: PyQt4 UI code generator 4.11.4 
# 
# WARNING! All changes made in this file will be lost! 
import sys 
import DBmanager as db 
from PyQt4 import QtCore, QtGui 
from newuser import Ui_Form 
from createprofile import Ui_StudentLogin 

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) 

#######SIGN IN/ LOG IN################################################################################################# 

class Ui_Login(QtGui.QWidget): 
    def __init__(self): 
     QtGui.QWidget.__init__(self) 
     self.dbu = db.DatabaseUtility('UsernamePassword_DB', 'masterTable') 
     self.setupUi(self) 
     self.confirm = None 

    def setupUi(self, Login): 
     Login.setObjectName(_fromUtf8("Form")) 
     Login.resize(400, 301) 
     self.label = QtGui.QLabel(Login) 
     self.label.setGeometry(QtCore.QRect(60, 60, 71, 21)) 
     font = QtGui.QFont() 
     font.setBold(True) 
     font.setWeight(75) 
     self.label.setFont(font) 
     self.label.setObjectName(_fromUtf8("label")) 
     self.label_2 = QtGui.QLabel(Login) 
     self.label_2.setGeometry(QtCore.QRect(60, 120, 81, 21)) 
     font = QtGui.QFont() 
     font.setBold(True) 
     font.setWeight(75) 
     self.label_2.setFont(font) 
     self.label_2.setObjectName(_fromUtf8("label_2")) 
     self.userName = QtGui.QLineEdit(Login) 
     self.userName.setGeometry(QtCore.QRect(140, 60, 151, 21)) 
     self.userName.setObjectName(_fromUtf8("userName")) 

     self.passWord = QtGui.QLineEdit(Login) 
     self.passWord.setGeometry(QtCore.QRect(140, 120, 151, 21)) 
     self.passWord.setObjectName(_fromUtf8("passWord")) 

     self.label_3 = QtGui.QLabel(Login) 
     self.label_3.setGeometry(QtCore.QRect(160, 10, 131, 21)) 
     font = QtGui.QFont() 
     font.setPointSize(10) 
     font.setBold(True) 
     font.setWeight(75) 
     self.label_3.setFont(font) 
     self.label_3.setObjectName(_fromUtf8("label_3")) 

     self.loginButton1 = QtGui.QPushButton(Login) 
     self.loginButton1.setGeometry(QtCore.QRect(40, 210, 75, 23)) 
     font = QtGui.QFont() 
     font.setBold(True) 
     font.setWeight(75) 
     self.loginButton1.setFont(font) 
     self.loginButton1.setObjectName(_fromUtf8("loginButton1")) 
     self.loginButton1.clicked.connect(self.login_Button1) 

     self.signUpButton = QtGui.QPushButton(Login) 
     self.signUpButton.setGeometry(QtCore.QRect(160, 210, 75, 23)) 
     font = QtGui.QFont() 
     font.setBold(True) 
     font.setWeight(75) 
     self.signUpButton.setFont(font) 
     self.signUpButton.setObjectName(_fromUtf8("signUpButton")) 
     self.signUpButton.clicked.connect(self.signUp_Button) 

     self.cancel1 = QtGui.QPushButton(Login) 
     self.cancel1.setGeometry(QtCore.QRect(280, 210, 75, 23)) 
     font = QtGui.QFont() 
     font.setBold(True) 
     font.setWeight(75) 
     self.cancel1.setFont(font) 
     self.cancel1.setObjectName(_fromUtf8("cancel1")) 
     self.connect(self, QtCore.SIGNAL('triggered()'), self.cancel_1) 

     self.retranslateUi(Login) 
     QtCore.QMetaObject.connectSlotsByName(Login) 

    def retranslateUi(self, Login): 
     Login.setWindowTitle(_translate("Form", "Login", None)) 
     self.label.setText(_translate("Form", "USERNAME", None)) 
     self.label_2.setText(_translate("Form", "PASSWORD", None)) 
     self.label_3.setText(_translate("Form", "LOGIN PAGE", None)) 
     self.loginButton1.setText(_translate("Form", "LOGIN", None)) 
     self.signUpButton.setText(_translate("Form", "SIGN UP", None)) 
     self.cancel1.setText(_translate("Form", "CANCEL", None)) 

    @QtCore.pyqtSignature("on_cancel1_clicked()") 
    def cancel_1(self): 
     self.close() 

    @QtCore.pyqtSignature("on_loginButton1_clicked()") 
    def login_Button1(self): 
     username = self.userName.text() 
     password = self.passWord.text() 
     if not username: 
      QtGui.QMessageBox.warning(self, 'Guess What?', 'Username Missing!') 
     elif not password: 
      QtGui.QMessageBox.warning(self, 'Guess What?', 'Password Missing!') 
     else: 
      self.AttemptLogin(username, password) 

    def AttemptLogin(self, username, password): 
     t = self.dbu.GetTable() 
     print (t) 
     for col in t: 
      if username == col[1]: 
       if password == col[2]: 
        QtGui.QMessageBox.information(self, 'WELCOME', 'Success!!') 
        self.createProfilePage() 
        self.close() 
       else: 
        QtGui.QMessageBox.warning(self, 'OOPS SORRY!', 'Password incorrect...') 
       return 


    def createProfilePage(self): 
     self.createprofile = Ui_StudentLogin() 
     self.createprofile.show() 


    @QtCore.pyqtSignature("on_signUpButton_clicked()") 
    def signUp_Button(self): 
     self.newuser = Ui_Form(self) 
     self.newuser.show() 

#######SIGN UP/ CREATE NEW USER################################################################################################# 


class Ui_Form(QtGui.QWidget): 
    def __init__(self,dbu): 
     QtGui.QWidget.__init__(self) 
     self.setupUi(self) 
     self.dbu = dbu 

    def setupUi(self, Form): 
     Form.setObjectName(_fromUtf8("Form")) 
     Form.resize(400, 300) 

     self.label = QtGui.QLabel(Form) 
     self.label.setGeometry(QtCore.QRect(60, 70, 51, 16)) 
     font = QtGui.QFont() 
     font.setBold(True) 
     font.setWeight(75) 
     self.label.setFont(font) 
     self.label.setObjectName(_fromUtf8("label")) 

     self.nameGet = QtGui.QLineEdit(Form) 
     self.nameGet.setGeometry(QtCore.QRect(120, 70, 191, 21)) 
     self.nameGet.setObjectName(_fromUtf8("nameGet")) 

     self.label_2 = QtGui.QLabel(Form) 
     self.label_2.setGeometry(QtCore.QRect(50, 120, 46, 13)) 
     font = QtGui.QFont() 
     font.setBold(True) 
     font.setWeight(75) 
     self.label_2.setFont(font) 
     self.label_2.setObjectName(_fromUtf8("label_2")) 

     self.label_3 = QtGui.QLabel(Form) 
     self.label_3.setGeometry(QtCore.QRect(30, 170, 71, 16)) 
     font = QtGui.QFont() 
     font.setBold(True) 
     font.setWeight(75) 
     self.label_3.setFont(font) 
     self.label_3.setObjectName(_fromUtf8("label_3")) 

     self.regNoGet = QtGui.QLineEdit(Form) 
     self.regNoGet.setGeometry(QtCore.QRect(120, 120, 191, 21)) 
     self.regNoGet.setObjectName(_fromUtf8("regNoGet")) 

     self.passWordGet = QtGui.QLineEdit(Form) 
     self.passWordGet.setGeometry(QtCore.QRect(120, 170, 191, 21)) 
     self.passWordGet.setObjectName(_fromUtf8("passWordGet")) 

     self.label_4 = QtGui.QLabel(Form) 
     self.label_4.setGeometry(QtCore.QRect(100, 20, 181, 21)) 
     font = QtGui.QFont() 
     font.setPointSize(10) 
     font.setBold(True) 
     font.setWeight(75) 
     self.label_4.setFont(font) 
     self.label_4.setObjectName(_fromUtf8("label_4")) 

     self.nextButton = QtGui.QPushButton(Form) 
     self.nextButton.setGeometry(QtCore.QRect(140, 250, 75, 23)) 
     font = QtGui.QFont() 
     font.setBold(True) 
     font.setWeight(75) 
     self.nextButton.setFont(font) 
     self.nextButton.setObjectName(_fromUtf8("nextButton")) 
     self.nextButton.clicked.connect(self.next_Button) 


     self.cancelButton = QtGui.QPushButton(Form) 
     self.cancelButton.setGeometry(QtCore.QRect(260, 250, 75, 23)) 
     font = QtGui.QFont() 
     font.setBold(True) 
     font.setWeight(75) 
     self.cancelButton.setFont(font) 
     self.cancelButton.setObjectName(_fromUtf8("cancelButton")) 
     self.cancelButton.clicked.connect(self.cancel_Button) 

     self.retranslateUi(Form) 
     QtCore.QMetaObject.connectSlotsByName(Form) 

    def retranslateUi(self, Form): 
     Form.setWindowTitle(_translate("Form", "New User", None)) 
     self.label.setText(_translate("Form", "NAME", None)) 
     self.label_2.setText(_translate("Form", "REG. NO", None)) 
     self.label_3.setText(_translate("Form", "PASSWORD", None)) 
     self.label_4.setText(_translate("Form", "   CREATE NEW USER", None)) 
     self.nextButton.setText(_translate("Form", "SUBMIT", None)) 
     self.cancelButton.setText(_translate("Form", "CANCEL", None)) 

    @QtCore.pyqtSignature("on_cancelButton_clicked()") 
    def cancel_Button(self): 
     self.close() 


    @QtCore.pyqtSignature("on_nextButton_clicked()") 
    def next_Button(self): 
     username = self.nameGet.text() 
     password = self.passWordGet.text() 
     regno = self.regNoGet.text() 
     if not username: 
      QtGui.QMessageBox.warning(self, 'Warning', 'Username Missing') 
     elif not password: 
      QtGui.QMessageBox.warning(self, 'Warning!', 'Password Missing') 
     elif not regno: 
      QtGui.QMessageBox.warning(self, 'Warning!', 'RegNo Missing') 
     else: 
      t = self.dbu.GetTable() 
      print (t) 
      for col in t: 
       if username == col[1]: 
        QtGui.QMessageBox.warning(self, 'Dang it!', 'Username Taken. :(') 
      else: 
       self.dbu.AddEntryToTable (username, password, regno) 
       QtGui.QMessageBox.information(self, 'Awesome!!', 'User Added SUCCESSFULLY!') 
       self.close() 



## def createProfileWindow(self): 
##  self.createprofile = Ui_StudentLogin() 
##  self.createprofile.show() 
## 
## def generate_report(self): 
##  data_line1 = self.nameGet.displayText() 
##  data_line2 = self.regNoGet.displayText() 
##  data_line3 = self.passWordGet.displayText() 
##  print data_line1 
##  print data_line2 
##  print data_line3 
##   




if __name__ == '__main__': 
    app = QtGui.QApplication(sys.argv) 
    ex = Ui_Login() 
    ex.show() 
    sys.exit(app.exec_()) 

回答

0

這個問題似乎是在您初始化Ui_Form。在這個片段中

@QtCore.pyqtSignature("on_signUpButton_clicked()") 
def signUp_Button(self): 
    self.newuser = Ui_Form(self) 
    self.newuser.show() 

它看起來像你逝去的self,在這種情況下,Ui_Login類的一個實例,作爲DBU到`Ui_Form」類的構造函數。相反,你應該像這樣傳遞dbu。

@QtCore.pyqtSignature("on_signUpButton_clicked()") 
    def signUp_Button(self): 
     self.newuser = Ui_Form(self.dbu) 
     self.newuser.show() 

你會發現很多的代碼,不叫出來的關鍵字參數在這樣的構造函數,但我覺得這是在維護代碼庫可讀不好的做法。所以我建議你今後要做這樣的事情來幫助你避免混淆。

self.newuser = Ui_Form(dbu=self.dbu) 
+0

非常感謝。有效。我會研究你的建議以備將來使用 – Kosi

0

你的代碼期望這一行:

db.DatabaseUtility('UsernamePassword_DB', 'masterTable') 

成與GetTable方法返回一個對象(以及對象分配給self.dbu)。因此,當你調用self.dbu.GetTable()你會得到一個錯誤,如果無論是從返回:

db.DatabaseUtility('UsernamePassword_DB', 'masterTable') 

沒有這種方法。所以檢查一下:

db.DatabaseUtility('UsernamePassword_DB', 'masterTable') 

實際返回並相應地調整您的代碼。