0.1 - Première fenêtre PyQt5 à l'aide de Qt Designer
0.2 - Code d'une première fenêtre PyQt5
1 - A propos de PyQt5
PyQt est une bibliothèque considéré comme une liaison du langage Python avec la boîte à outils GUI toolkit Qt, qui peut être installée rapidement avec l'utilitaire pip et implémentée en tant que module Python. PyQt est un logiciel gratuit développé par la firme Riverbank Computing. Il est disponible dans des conditions similaires aux versions Qt antérieures et cela signifie une variété de licences, dont la GNU Général Public Licence (GPL) et la licence commerciale, mais pas la GNU Lesser Général Public Licence (LGPL). PyQt prend en charge Microsoft Windows ainsi que diverses versions d'UNIX, y compris Linux et MacOS (ou Darwin). PyQt implémente environ 440 classes et plus de 6 000 fonctions et méthodes.
2 - Installation de PyQt5 et premier programme
2.1 - Installation de PyQt5
La bibliothèque PyQt5, peut être installé facilement via l'utilitaire pip:
Après avoir installé la bibliothèque PyQt5, on dois installer les outils auxiliaires tools à l'aide de l'invite de commande:
2.2 - Première fenêtre graphique PyQt5
Pour créer une fenêtre graphique en PyQt5, on dois:
- Importer le module système : sys
- Importer la classe qui génère l'application : QApplications depuis le package PyQt5.QtWidgets
- Importer la classe QWidget depuis le package PyQt5.QtWidgets
- Créer une application à l'aide de la méthode instance() de la classe QApplication
- Créer une fenêtre avec la méthode QWidget(): fen = QWidget()
- Visualiser la fenêtre à l'aide de la méthode show() : fen.show()
- Exécuter l'application à l'aide de la méthode exec_() : app.exec_()
Code première fenêtre graphique avec PyQt5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import sys from PyQt5.QtWidgets import QApplication, QWidget # create a PyQt5 application app = QApplication(sys.argv) # create a QWidget object win = QWidget() # Make a window title win.setWindowTitle("This is a first PyQt5 app !") # define a geometry of the window win.setGeometry(100 , 100 , 500 , 400) # apply the show method to view the window win.show() sys.exit(app.exec_()) |
Ce qui affiche une jolie fenêtre à l'exécution:
3 - Première fenêtre PyQt5 selon l'approche objet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import sys from PyQt5.QtWidgets import QApplication, QWidget class MyWindow(QWidget): def __init__(self , win ): super().__init__() self.win = win def build(self): self.win.setWindowTitle("PyQt5 from object approach") self.win.setGeometry(100 , 100 , 400 , 150) if __name__ == '__main__': app = QApplication(sys.argv) root = QWidget() mywin = MyWindow(root) mywin.build() root.show() sys.exit(app.exec_()) |
4 - Première fenêtre générée par Qt Designer
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 |
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'first.ui' # Created by: PyQt5 UI code generator 5.15.4 # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(533, 371) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec_()) |
Younes Derfoufi
CRMEF OUJDA
1 thought on “Première Fenêtre Graphique PyQt5”