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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| #!/usr/bin/env python3
# coding: utf-8
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
# L'objet pour gérer mon appli
class myAppli(QApplication):
# Constructeur
def __init__(self, *args, **kwargs):
# Appel méthode objet hérité
super().__init__(*args, **kwargs)
# Le widget principal
self.__mainWidget=myMainWindow()
# __init__()
# Lancement boucle de traitement des évènements Qt
def exec_(self):
# Affichage du widget principal
self.__mainWidget.show()
# Appel méthode objet hérité
return super().exec_()
# exec_()
# class myAppli
# L'objet pour gérer ma fenêtre principale
class myMainWindow(QMainWindow):
# Constructeur
def __init__(self, *args, **kwargs):
# Appel méthode objet hérité
super().__init__(*args, **kwargs)
# La fenêtre sera détruite à sa fermeture
self.setAttribute(Qt.WA_DeleteOnClose)
# Le widget associé au mainWindow
self.setCentralWidget(QWidget(parent=self))
# Le bouton
btn=QPushButton("Clients", parent=self.centralWidget(), clicked=self.__slotClient)
quitter=QPushButton("Quitter", parent=self.centralWidget(), clicked=self.close)
# Le gestionnaire de positionnement principal
mainLayout=QVBoxLayout(self.centralWidget())
mainLayout.addWidget(btn, stretch=0)
mainLayout.addStretch(1)
mainLayout.addWidget(quitter, stretch=0)
# __init__()
# Le slot appel du client
@pyqtSlot()
def __slotClient(self):
# Boite de connexion
print("client")
self.__xxx=myConnect(login="Hello", pwd="Word")
res=self.__xxx.exec_()
print("entrée ok" if res else "entrée refusée")
# __slotClient
# class myMainWindow
# L'objet pour gérer ma fenêtre de connexion
class myConnect(QDialog):
# Constructeur
def __init__(self, login, pwd, *args, **kwargs):
# Appel méthode objet hérité
super().__init__(*args, **kwargs)
# La fenêtre sera détruite à sa fermeture
self.setAttribute(Qt.WA_DeleteOnClose)
# La fenêtre est modale
self.setModal(True)
# Login et mot de passe
self.__param=(login, pwd)
# La zone login
loginLabel=QLabel("login", parent=self)
self.__loginText=QLineEdit(login, parent=self)
loginLayout=QHBoxLayout()
loginLayout.addWidget(loginLabel, stretch=0)
loginLayout.addWidget(self.__loginText, stretch=1)
# La zone pwd
pwdLabel=QLabel("password", parent=self)
self.__pwdText=QLineEdit(pwd, parent=self)
pwdLayout=QHBoxLayout()
pwdLayout.addWidget(pwdLabel, stretch=0)
pwdLayout.addWidget(self.__pwdText, stretch=1)
# Les boutons
valid=QPushButton("Valider", parent=self, clicked=self.__slotCheck)
annul=QPushButton("Annuler", parent=self, clicked=self.close)
actionLayout=QHBoxLayout()
actionLayout.addStretch(1)
actionLayout.addWidget(valid, stretch=0)
actionLayout.addWidget(annul, stretch=0)
actionLayout.addStretch(1)
# Le gestionnaire de positionnement principal
mainLayout=QVBoxLayout(self)
mainLayout.addLayout(loginLayout, stretch=0)
mainLayout.addLayout(pwdLayout, stretch=0)
mainLayout.addLayout(actionLayout, stretch=0)
mainLayout.addStretch(1)
# __init__()
# Evènement fermeture fenêtre (méthode surchargée)
def closeEvent(self, event):
print(
"%s => %s.closeEvent(%s)" % (
self.sender().__class__.__name__,
self.__class__.__name__,
"%s (%s)" % (event.__class__.__name__, type(event)),
)
)
# Confirmation quitter
if QMessageBox.question(
self,
"Confirmation",
"Confirmez-vous vouloir quitter ?",
QMessageBox.Yes|QMessageBox.No,
) != QMessageBox.Yes:
# Refus de l'évènement (refus de la fermeture)
event.ignore()
# La fonction s'arrête là
return
# if
self.reject()
# closeEvent()
# Check mot de passe
@pyqtSlot()
def __slotCheck(self):
if (self.__loginText.text(), self.__pwdText.text()) == self.__param:
QMessageBox.information(
self,
"Accepté",
"Entrée ok",
QMessageBox.Ok,
)
self.accept()
else:
QMessageBox.warning(
self,
"Refusé",
"Entrée refusée",
QMessageBox.Ok,
)
self.reject()
# if
# __slotCheck()
# class myConnect
if __name__ == "__main__":
sys.exit(myAppli(sys.argv).exec_()) |
Partager