Wednesday, May 11, 2022

Upgrade 02: PyQt6 Desktop App Template

This demo program add another improvement of the desk top app template(original post here). The newly added feature is a the ability to login, a simple password encryption(sha256) and connecting to sa mysql database. Also added a top layer file that should perform system configuration before the splash screen is shown.

 

Login Info:

  UserName : admin

  Password :  Appl3Tr33.456

The output:

 


The mysql table:

 

1
2
3
4
5
6
7
CREATE TABLE `tbusr` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `username` varchar(256) NOT NULL,
 `userpassword` varchar(2048) NOT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `ind_usr` (`username`)
 ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1

 

The code:

The python code to encrypt the password:

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import os
import hashlib
import hmac
password = "Appl3Tr33.456"
password = password.encode()
#salt = os.urandom(16)
password_hash = hashlib.pbkdf2_hmac("sha256", password, salt, 100000)

salt = b'4\xa8y\x8e\xca\xfb\x7f\x8e\xd5\x97v\x14\xc7[Z\xd0'
print(salt)
print(password_hash)

popular_app.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from imports import *
from splash import SplashScreen
class App(a.QWidget):
    def __init__(self, parent=None):
        super().__init__()
        self.setWindowTitle("")
        self.do_logging()
        self.setFixedSize(0, 0)
        self.hide()
        t.sleep(1)
        self.splash1 = SplashScreen()
        self.splash1.show() 
    def do_logging(self):
        print("Do something here...")
 
        
def main():

    app = a.QApplication(s.argv)
    app.setStyleSheet('''
        #title_label {
            font-size: 50px;
            color: #F0B21A;
        }
        #desc_label {
            font-size: 15px;
            color: #F0B21A;
        }
        #loading_label {
            font-size: 20px;
            color: #F0B21A;
        }
        QFrame {
            background-color: #2F339B;
            color: #c8c8c8;
        }
    ''')
    ex = App()
    s.exit(app.exec())


if __name__ == '__main__':
    main()


login.py:


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from imports import *
import mysql.connector as mysql
import hashlib
import hmac
import pandas as pd
from mainw import Window2

TIME_LIMIT = 100
class WindowApp(a.QMainWindow):
    def __init__(self, parent=None):
        super().__init__()
        self.setWindowTitle("Login")
        self.setFixedSize(320, 245)
        self.quit = g.QAction("Quit", self)
        self.quit.triggered.connect(self.closeEvent)
        self.move(400, 200) 
        lblusr =  a.QLabel('UserName', self)
        lblusr.setStyleSheet('QLabel {background-color: transparent; color: black;}')
        lblusr.setGeometry(25, 55, 100, 30) 
        self.txtusr = a.QLineEdit('', self) 
        
        self.txtusr.setGeometry(100, 55, 150, 30)  
        lblpwd =  a.QLabel('Password', self)
        lblpwd.setStyleSheet('QLabel {background-color: transparent; color: black;}')
        lblpwd.setGeometry(25, 100, 100, 30) 
        self.txtpwd = a.QLineEdit('', self) 
        self.txtusr.setPlaceholderText("Enter Username Here")
        self.txtpwd.setPlaceholderText("Enter Password Here")
        self.txtpwd.setGeometry(100, 100, 150, 30) 
        self.txtpwd.setEchoMode(a.QLineEdit.EchoMode.Password)        
        self.pblogin = a.QPushButton('Login', self)
        self.pblogin.setEnabled(False)
        self.pblogin.setGeometry(100, 165, 65, 25)
        self.pblogin.clicked.connect(self.onClick_pb3)  
        self.txtpwd.textChanged.connect(self.on_text_changed)
        self.txtusr.textChanged.connect(self.on_text_changed)
        pbcanc = a.QPushButton('Cancel', self)
        pbcanc.setGeometry(180, 165, 65, 25)
        pbcanc.clicked.connect(self.onClick_pb4)  
        self.statusBar = a.QStatusBar()
        self.setStatusBar(self.statusBar)
        self.statusBar.showMessage("Ready", 5000)        
        #self.show()
        
        self.progressBar = a.QProgressBar()
        self.progressBar.setStyleSheet("""QProgressBar {
            background-color: transparent;
            color: #9B2F6A;
            border-style: none;
            border-radius: 5px;
            text-align: center;
            font-size: 10px;
        }
        QProgressBar::chunk {
            border-radius: 5px;
            background-color: qlineargradient(spread:pad x1:0, x2:1, y1:0.511364, y2:0.523, stop:0 #E1F01A);
        }""")
        self.statusBar.addPermanentWidget(self.progressBar)
        self.progressBar.move(30, 40)
        self.progressBar.setFixedSize(120, 20)
        #self.progressBar.setValue(100)
    def on_text_changed(self):
        self.pblogin.setEnabled(bool(self.txtusr.text()) and bool(self.txtpwd.text()))
        
    def closeEvent(self, event):
        close = a.QMessageBox()
        close.setIcon(a.QMessageBox.Icon.Question)
        close.setWindowTitle('Pls confirm')
        close.setStyleSheet('QLabel {background-color: transparent; color: black;}')
        close.setText("Do you really want to quit?")
        close.setStandardButtons(a.QMessageBox.StandardButton.Yes | a.QMessageBox.StandardButton.Cancel)
        close = close.exec()

        if close == a.QMessageBox.StandardButton.Yes:
            event.accept()
        else:
            event.ignore()
    def onClick_pb3(self):
        db = mysql.connect(
             host = "localhost",
             user = "root",
             passwd = "",
             database = "pop_app"
        )
        cursor = db.cursor()
        t1 = self.txtusr.text()
        t2 = self.txtpwd.text()
        salt = b'4\xa8y\x8e\xca\xfb\x7f\x8e\xd5\x97v\x14\xc7[Z\xd0'
        print(t2)
        t2 = hashlib.pbkdf2_hmac("sha256", t2.encode(), salt, 100000)
        
        t1 = repr(str(t1))
        t2 = repr(str(t2))
        print(t2)
        query = "SELECT * FROM tbusr where username = %s and userpassword  = %s" %(t1, t2)
        cursor.execute(query)
        records = cursor.fetchall()
        df = pd.DataFrame(records) 
        count = 0
        while count < TIME_LIMIT:
            count += 1
            t.sleep(0.01)
            self.progressBar.setValue(count)
        if df.empty:    
            self.statusBar.showMessage("User not found!", 5000)
            self.txtusr.setStyleSheet('QLineEdit {background-color: #ffe6e6; color: black;}')
            self.txtusr.repaint()
            t.sleep(1.5)  
            self.txtusr.setStyleSheet('QLineEdit {background-color: white; color: black;}')
            self.txtusr.repaint()
        else:
            self.statusBar.showMessage("User found!", 5000)
            self.statusBar.repaint()
            t.sleep(2)
            self.showWindow2()
            
            self.hide()
            
        #self.w = Window2()       
        #self.w.show()
        
    def showWindow2(self):
        self.mdiwindow = Window2()
        self.mdiwindow.show()
    def onClick_pb4(self):
        self.quit 
        self.close()    
          
if __name__ == "__main__":       
    app = a.QApplication(s.argv)
    app.setStyleSheet('''        
        QProgressBar {
            background-color: #000000;
            color: #9B2F6A;
            border-style: none;
            border-radius: 4px;
            text-align: center;
            font-size: 10px;
        }
        QProgressBar::chunk {
            border-radius: 4px;
            background-color: qlineargradient(spread:pad x1:0, x2:1, y1:0.511364, y2:0.523, stop:0 #E1F01A);
        }
    ''')
    splash = WindowApp()
    splash.show()
    s.exit(app.exec())

No comments:

Post a Comment