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
| #!/usr/bin/env python
import os
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *
import dbModel
import accueilA
import accueilB
import accueilC
class Login(QDialog):
def __init__(self, parent=None):
super(Login, self).__init__(parent)
self.setWindowTitle(u"Connexion ....")
self.edtUsername = QLineEdit(self)
self.edtUsername.setText(u"identifiant?")
self.edtUsername.returnPressed.connect(self.close)
self.edtPassword = QLineEdit(self)
self.edtPassword.setText(u"mot de passe?")
self.edtPassword.setEchoMode(QLineEdit.Password) # ne pas afficher le mot de passe
self.edtPassword.returnPressed.connect(self.close)
self.btnConnexion = QPushButton('Connexion', self)
self.btnConnexion.clicked.connect(self.close)
layout = QFormLayout()
layout.addRow("Nom d'utilisateur : ",self.edtUsername)
layout.addRow("Mot de passe : ", self.edtPassword)
btnAnnuler = QPushButton("Annuler")
btnAnnuler.clicked.connect(self.close)
l = QHBoxLayout()
l.addStretch()
l.addWidget(self.btnConnexion)
l.addWidget(btnAnnuler)
layout.addRow(l)
self.setLayout(layout)
self.edtUsername.setFocus()
self.edtUsername.selectAll()
def closeEvent(self, event):
user = self.edtUsername.text()
passe = self.edtPassword.text()
print("Username : %s et le password : %s "%(user,passe))
self.cv = dbModel.isElementOf(user,passe)
if self.cv == 1 :
ecran = accueilA.MainWindow()
ecran.show()
elif self.cv == 2:
ecran = accueilB.MainWindow()
ecran.show()
elif self.cv == 3:
ecran = accueilC.MainWindow()
ecran.show()
else:
QMessageBox.critical(self, "Database Connection",
"Database Error: Nom d'utilisateur ou mot de passe incorrect!")
# acceptation de la fermeture de la fenêtre
event.accept()
def main():
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
print("Connecting to database...")
db = QSqlDatabase.addDatabase("QPSQL")
db.setHostName("localhost")
db.setDatabaseName("MABASE")
db.setUserName("name")
db.setPassword("*****")
#
ok = db.open()
if ok:
print("About to handle database stuff")
app.processEvents()
#dbModel.createDBTables()
else:
QMessageBox.warning(None, "Database Connection",
QString("Database Error: %1").arg(db.lastError().text()))
sys.exit(1)
print("Database not handle")
cnx = Login()
cnx.exec_()
sys.exit(app.exec_())
main() |
Partager