AttributeError:'Ui_Login'对象没有属性'GetTable'
问题描述:
我创建了一个登录和注册页面,使用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_())
答
这个问题似乎是在您初始化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)
答
你的代码期望这一行:
db.DatabaseUtility('UsernamePassword_DB', 'masterTable')
成与GetTable
方法返回一个对象(以及对象分配给self.dbu
)。因此,当你调用self.dbu.GetTable()
你会得到一个错误,如果无论是从返回:
db.DatabaseUtility('UsernamePassword_DB', 'masterTable')
没有这种方法。所以检查一下:
db.DatabaseUtility('UsernamePassword_DB', 'masterTable')
实际返回并相应地调整您的代码。
非常感谢。有效。我会研究你的建议以备将来使用 – Kosi