Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from PyQt5.QtWidgets import QWidget, QApplication,QLineEdit, QPushButton, QMessageBox
- import MySQLdb as mdb
- class Login(QWidget):
- def __init__(self):
- super().__init__()
- self.title = 'Login'
- self.top = 100
- self.left = 100
- self.width = 400
- self.height = 400
- self.Log()
- def Log(self):
- self.setWindowTitle(self.title)
- self.setGeometry(self.top, self.left, self.height, self.width)
- self.host = QLineEdit(self)
- self.host.setPlaceholderText('Host')
- self.host.move(100, 100)
- self.user = QLineEdit(self)
- self.user.setPlaceholderText('User Name')
- self.user.move(100, 150)
- self.pw = QLineEdit(self)
- self.pw.setPlaceholderText('Password')
- self.pw.setEchoMode(QLineEdit.Password)
- self.pw.move(100, 200)
- self.db = QLineEdit(self)
- self.db.setPlaceholderText('Database')
- self.db.move(100, 250)
- btn = QPushButton('Click to login', self)
- btn.move(100, 300)
- btn.clicked.connect(self.connectDB) # Here I want it to close that login screen and opens the insert screen after it logs in
- self.show()
- def connectDB(self):
- input_host = self.host.text()
- input_user = self.user.text()
- input_password = self.pw.text()
- input_database = self.db.text()
- try:
- con = mdb.connect(input_host, input_user, input_password, input_database)
- except:
- QMessageBox.about(self, 'Connection','Failed')
- self.show()
- class Insert(QWidget):
- def __init__(self):
- super().__init__()
- self.title = 'Login'
- self.top = 100
- self.left = 100
- self.width = 400
- self.height = 400
- self.Input()
- def Input(self):
- self.setWindowTitle(self.title)
- self.setGeometry(self.top, self.left, self.height, self.width)
- self.user = QLineEdit(self)
- self.user.setPlaceholderText('Name')
- self.user.move(100, 100)
- self.pw = QLineEdit(self)
- self.pw.setPlaceholderText('Job')
- self.pw.move(100, 150)
- btn = QPushButton('Insert', self)
- btn.move(100, 200)
- btn.clicked.connect(self.insertDB)
- self.show()
- def insertDB(self):
- input_name = self.user.text()
- input_job = self.pw.text()
- with self.con: # Not sure about this. Highly likely it's wrong. I want it to use the con from the first class to create the cursor
- cursor = self.con.cursor()
- cursor.execute("INSERT INTO USERS(Name, Job)"
- "VALUES('%s', '%s')" % (input_name, input_job))
- QMessageBox.about(self, 'Process', 'Data Inserted')
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- ex = Login()
- sys.exit(app.exec_())
- with self.con:
- cursor = self.con.cursor()
- cursor.execute("INSERT INTO USERS(Name, Job)"
- "VALUES('%s', '%s')" % (input_name, input_job))
- QMessageBox.about(self, 'Process', 'Data Inserted')
- self.con = mdb.connect(input_host, input_user, input_password, input_database)
- def __init__(self, connection):
- super().__init__()
- self.con = connection
- self.title = 'Login'
- self.top = 100
- self.left = 100
- self.width = 400
- self.height = 400
- self.Input()
Add Comment
Please, Sign In to add comment