Guest User

Untitled

a guest
Jun 18th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. import sys
  2. from PyQt5.QtWidgets import QWidget, QApplication,QLineEdit, QPushButton, QMessageBox
  3.  
  4. import MySQLdb as mdb
  5.  
  6. class Login(QWidget):
  7.  
  8. def __init__(self):
  9. super().__init__()
  10. self.title = 'Login'
  11. self.top = 100
  12. self.left = 100
  13. self.width = 400
  14. self.height = 400
  15. self.Log()
  16.  
  17.  
  18. def Log(self):
  19. self.setWindowTitle(self.title)
  20. self.setGeometry(self.top, self.left, self.height, self.width)
  21.  
  22. self.host = QLineEdit(self)
  23. self.host.setPlaceholderText('Host')
  24. self.host.move(100, 100)
  25. self.user = QLineEdit(self)
  26. self.user.setPlaceholderText('User Name')
  27. self.user.move(100, 150)
  28. self.pw = QLineEdit(self)
  29. self.pw.setPlaceholderText('Password')
  30. self.pw.setEchoMode(QLineEdit.Password)
  31. self.pw.move(100, 200)
  32. self.db = QLineEdit(self)
  33. self.db.setPlaceholderText('Database')
  34. self.db.move(100, 250)
  35.  
  36. btn = QPushButton('Click to login', self)
  37. btn.move(100, 300)
  38. btn.clicked.connect(self.connectDB) # Here I want it to close that login screen and opens the insert screen after it logs in
  39.  
  40. self.show()
  41.  
  42. def connectDB(self):
  43.  
  44. input_host = self.host.text()
  45. input_user = self.user.text()
  46. input_password = self.pw.text()
  47. input_database = self.db.text()
  48. try:
  49. con = mdb.connect(input_host, input_user, input_password, input_database)
  50.  
  51. except:
  52. QMessageBox.about(self, 'Connection','Failed')
  53.  
  54. self.show()
  55.  
  56.  
  57.  
  58. class Insert(QWidget):
  59.  
  60. def __init__(self):
  61. super().__init__()
  62. self.title = 'Login'
  63. self.top = 100
  64. self.left = 100
  65. self.width = 400
  66. self.height = 400
  67. self.Input()
  68. def Input(self):
  69. self.setWindowTitle(self.title)
  70. self.setGeometry(self.top, self.left, self.height, self.width)
  71.  
  72. self.user = QLineEdit(self)
  73. self.user.setPlaceholderText('Name')
  74. self.user.move(100, 100)
  75. self.pw = QLineEdit(self)
  76. self.pw.setPlaceholderText('Job')
  77. self.pw.move(100, 150)
  78.  
  79. btn = QPushButton('Insert', self)
  80. btn.move(100, 200)
  81. btn.clicked.connect(self.insertDB)
  82.  
  83. self.show()
  84.  
  85. def insertDB(self):
  86.  
  87. input_name = self.user.text()
  88. input_job = self.pw.text()
  89.  
  90. 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
  91. cursor = self.con.cursor()
  92. cursor.execute("INSERT INTO USERS(Name, Job)"
  93. "VALUES('%s', '%s')" % (input_name, input_job))
  94.  
  95. QMessageBox.about(self, 'Process', 'Data Inserted')
  96.  
  97.  
  98. if __name__ == '__main__':
  99. app = QApplication(sys.argv)
  100. ex = Login()
  101. sys.exit(app.exec_())
  102.  
  103. with self.con:
  104. cursor = self.con.cursor()
  105. cursor.execute("INSERT INTO USERS(Name, Job)"
  106. "VALUES('%s', '%s')" % (input_name, input_job))
  107.  
  108. QMessageBox.about(self, 'Process', 'Data Inserted')
  109.  
  110. self.con = mdb.connect(input_host, input_user, input_password, input_database)
  111.  
  112. def __init__(self, connection):
  113. super().__init__()
  114. self.con = connection
  115. self.title = 'Login'
  116. self.top = 100
  117. self.left = 100
  118. self.width = 400
  119. self.height = 400
  120. self.Input()
Add Comment
Please, Sign In to add comment