Kosi Kosi - 3 months ago 14
Python Question

AttributeError: 'Ui_Login' object has no attribute 'GetTable'

Im creating a login and signup page that links to other GUI using PyQt. Most of the code works but when i try to sign up a new user, it gives me a AttributeError: 'Ui_Login' object has no attribute 'GetTable'. GetTable is defined in the code for the databse created with MySQl whic is called into the Login class and Signup class.
Please in new to python and programming in general. Sorry if this question seems daft. but i have read a lot on previously asked ones and i cant seem to understand what it is saying. Thanks

# -*- 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_())

Answer

The issue seems to be in your initialization of the Ui_Form. In this snippet

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

it looks like you are passing self, in this case an instance of the Ui_Login class, as the dbu to the constructor of the `Ui_Form' class. Instead you should pass the dbu like so.

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

You will find a lot of code that doesn't call out keyword arguments in constructors like this, but I find it to be bad practice in maintaining a readable codebase. So I would advise you to do something like this in future to help you avoid confusion.

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