Advertisement
Rendier

RFI example

Mar 31st, 2019
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.69 KB | None | 0 0
  1. from PyQt5.QtWidgets import *
  2. from PyQt5.QtCore import *
  3. from PyQt5.QtGui import *
  4. from PyQt5.uic import loadUiType
  5.  
  6. import os
  7. from os import path  # to access the directory path in windows
  8. import sys  # this is for system
  9.  
  10. import pymysql
  11.  
  12. FROM_CLASS, _ = loadUiType(path.join(path.dirname(__file__), "Main_rfi-design2.ui"))
  13. FROM_CLASS2, _ = loadUiType(path.join(path.dirname(__file__), "login_rfi.ui"))
  14.  
  15. class Login(QMainWindow, FROM_CLASS2):
  16.     def __init__(self, parent=None):
  17.         super(Login, self).__init__(parent)
  18.         QMainWindow.__init__(self)
  19.        
  20.         self.parent = parent
  21.  
  22.         self.setupUi(self)
  23.         self.btn_login.clicked.connect(self.handel_login)
  24.         self.window2 = None
  25.  
  26.     def handel_login(self):
  27.         self.db = pymysql.connect(host="localhost", user="root", passwd="", db="standard_co")
  28.         self.cur = self.db.cursor()
  29.  
  30.         self.user_name = self.lineEdit.text()
  31.         self.password = self.lineEdit_16.text()
  32.  
  33.         sql = '''SELECT user_name FROM users'''
  34.         self.cur.execute(sql)
  35.         users = self.cur.fetchall()
  36.         users = [i for sub in users for i in sub]  # another way to convert to list from tuple
  37.         print('User Names= ', users)
  38.  
  39.         sql2 = '''SELECT pass FROM users'''
  40.         self.cur.execute(sql2)
  41.         pass1 = self.cur.fetchall()
  42.         pass1 = [i for sub in pass1 for i in sub]  # another way to convert to list from tuple
  43.         print('Pass= ', pass1)
  44.         if self.user_name in users:
  45.             user_index = users.index(self.user_name)
  46.             if self.password == pass1[user_index]:
  47.                 self.window2 = Main()
  48.                 # self.window2 = Main(Login.return_username())
  49.                 self.close()
  50.                 self.window2.show()
  51.             else:
  52.                 self.label.setText('Please check User Name or Password...')
  53.         else:
  54.             self.label.setText('User name dose not exist...')
  55.             print('Current user is: ' + self.user_name)
  56.             # return self.user_name
  57.            
  58.             ## you can use self.parent here to directly change the variable
  59.             ## self.user_name in Main() like this.  You must define self.user_name in Main() first to do this.
  60.             self.parent.user_name = self.user_name
  61.            
  62.     def return_username(self):
  63.         return self.user_name
  64.  
  65. class Main(QMainWindow, FROM_CLASS):
  66.  
  67.     def __init__(self, parent=None):
  68.         super(Main, self).__init__(parent)
  69.         QMainWindow.__init__(self)
  70.         # this is stored in the UI file to setup:
  71.         self.setupUi(self)
  72.        
  73.         ## by adding a blank user_name here, will allow you to modify it
  74.         ## from within Login by using self.parent.user_name = 'user_name'
  75.         self.user_name = ""
  76.  
  77.         # To start applaying functions:
  78.         self.initUI()
  79.         self.handel_button()
  80.         self.standard_combo_box()
  81.         # self.handel_db_connection()
  82.        
  83.         ## Put Login Here and pass self to login window as parent
  84.         login = Login(parent=self)
  85.         login.show()
  86.        
  87.         ## Since you are calling Login() from here,
  88.         ## you can access the return_username() function to assign it to
  89.         ## the class variable self.user_name
  90.         self.user_name = login.return_username()
  91.  
  92. def main():
  93.     app = QApplication(sys.argv)
  94.    
  95.     ## Change your first window to Main() instead of Login()
  96.     ## If the login fails, have a 'login failed' screen for mainwindow to show and quit
  97.     ## window = Login()
  98.     window = Main()
  99.     window.show()  # to show the window
  100.     app.exec()  # infinte (App loop) loop to maintain the window open
  101.  
  102.  
  103. if __name__ == '__main__':
  104.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement