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 163 164 165 166 167 168 169 170 171 172
| #!/usr/bin/env python
# coding: utf-8
from PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *
# L'objet pour gérer mon appli
class myAppli(QApplication):
# Constructeur
def __init__(self, *args, **kwargs):
# Environnement ihm
self.ihm={
"appli" : self,
"self" : self,
"mainWid" : None,
}
# Appel méthode objet hérité
super().__init__(*args, **kwargs)
# Le widget principal
self.ihm["mainWid"]=myMainWindow()
# __init__()
# Lancement boucle de traitement des évènements Qt
def exec(self):
# Affichage du widget principal
self.ihm["mainWid"].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):
# Récupération environnement ihm appli
self.__ihm=QApplication.instance().ihm
self.__ihm["mainWid"]=self
# Appel méthode objet hérité
super().__init__(*args, **kwargs)
# La fenêtre sera détruite à sa fermeture
self.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose)
# La zone centrale
self.setCentralWidget(QtGrappe(parent=self))
# __init__()
# class myMainWindow
# La grappe
class QtGrappe(QWidget):
# Constructeur
def __init__(self, *args, **kwargs):
# Récupération environnement ihm appli
self.__ihm=QApplication.instance().ihm
# Appel méthode objet hérité
super().__init__(*args, **kwargs)
# La fenêtre sera détruite à sa fermeture
self.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose)
# La saisie
self.__saisie=QtSaisie(parent=self, static=True)
self.__saisie.sigClose[dict].connect(self.__slotSaisie)
# Le bouton connecté à la fermeture des fenêtres de l'application
btn=QPushButton("Quit", parent=self, clicked=self.__ihm["mainWid"].close)
btn.setFont(QFont("Times", 18, QFont.Weight.Bold))
# Le bouton d'action
action=QPushButton("saisie", parent=self, clicked=self.__saisie.show)
action.setFont(QFont("Times", 18, QFont.Weight.Bold))
# Le gestionnaire de positionnement principal
mainLayout=QVBoxLayout(self)
mainLayout.addStretch(1)
mainLayout.addWidget(action, stretch=0)
mainLayout.addStretch(1)
mainLayout.addWidget(btn, stretch=0)
# __init__()
# Récupération valeurs saisies
@pyqtSlot(dict)
def __slotSaisie(self, data):
print("data by signal=", data)
print("data by property=", self.__saisie.property("data"))
# class QtGrappe
# Zone de saisie
class QtSaisie(QDialog):
# Le signal émis à la fermeture
sigClose=pyqtSignal(dict)
# Constructeur
def __init__(self, *args, **kwargs):
# Récupération environnement ihm appli
self.__ihm=QApplication.instance().ihm
# Récupérations paramètres spécifiques
static=kwargs.pop("static", True)
# Appel méthode objet hérité
super().__init__(*args, **kwargs)
# En cas de non statique, la fenêtre sera détruite à sa fermeture
if not static: self.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose)
# La fenêtre est modale
self.setModal(True)
# La saisie
nomLabel=QLabel("nom", parent=self)
self.__nomEdit=QLineEdit(parent=self)
prenomLabel=QLabel("prenom", parent=self)
self.__prenomEdit=QLineEdit(parent=self)
adresseLabel=QLabel("adresse", parent=self)
self.__adresseEdit=QLineEdit(parent=self)
# Le gestionnaire de positionnement principal
mainLayout=QGridLayout(self)
mainLayout.addWidget(nomLabel, 0, 0, 1, 1)
mainLayout.addWidget(self.__nomEdit, 0, 1, 1, 1)
mainLayout.addWidget(prenomLabel, 1, 0, 1, 1)
mainLayout.addWidget(self.__prenomEdit, 1, 1, 1, 1)
mainLayout.addWidget(adresseLabel, 2, 0, 1, 1)
mainLayout.addWidget(self.__adresseEdit, 2, 1, 1, 1)
mainLayout.addWidget(
QPushButton("Ok", clicked=self.close),
3, 0, 1, 2,
)
# __init__()
# A l'ouverture de la fenêtre
def showEvent(self, e):
super().showEvent(e)
# Récupération des valeurs
if (data:=self.property("data")) is None: return
self.__nomEdit.setText(data["nom"])
self.__prenomEdit.setText(data["prenom"])
self.__adresseEdit.setText(data["adresse"])
# showEvent()
# A la fermeture de la fenêtre
def closeEvent(self, e):
# Sauvegarde des valeurs
data={
"nom" : self.__nomEdit.text().strip(),
"prenom" : self.__prenomEdit.text().strip(),
"adresse" : self.__adresseEdit.text().strip(),
}
self.setProperty("data", data)
# Emission du signal de fermeture
self.sigClose[dict].emit(data)
# Méthode objet hérité
super().closeEvent(e)
# closeEvent()
# class QtSaisie
# Dans le cas d'une exécution directe du script
if __name__ == "__main__":
# L'application principale Qt
import sys
sys.exit(myAppli(sys.argv).exec()) |
Partager