Tutoriel PyQt5 - Fenêtre de base

Jinku Hu 30 janvier 2023
  1. Fenêtre de base PyQt5
  2. PyQt5 Changer la taille de la fenêtre
  3. PyQt5 Ajouter une icône de fenêtre
Tutoriel PyQt5 - Fenêtre de base

Fenêtre de base PyQt5

Nous allons créer une fenêtre de base dans PyQt5.

Python
 pythonCopyimport sys
from PyQt5 import QtWidgets


def basicWindow():
    app = QtWidgets.QApplication(sys.argv)
    windowExample = QtWidgets.QWidget()
    windowExample.setWindowTitle("Basic Window Example")
    windowExample.show()
    sys.exit(app.exec_())


basicWindow()
Python
 pythonCopyfrom PyQt5 import QtWidgets

Il importe le module QtWidgets pour que nous ayons accès à l’interface graphique.

Python
 pythonCopyapp = QtWidgets.QApplication(sys.argv)

Il crée un objet d’application qui a accès à la boucle d’événements.

Python
 pythonCopywindowExample = QtWidgets.QWidget()

Nous devons ensuite créer un QtWidget, parce que nous allons utiliser cela comme notre fenêtre de haut niveau et il a tout ce que nous voulons.

Python
 pythonCopywindowExample.setWindowTitle("Basic Window Example")

setWindowTitle donne un titre à la fenêtre et elle peut être appelée quand vous en avez besoin.

Python
 pythonCopywindowExample.show()

Il est nécessaire pour montrer la fenêtre.

Python
 pythonCopysys.exit(app.exec_())

Nous avons besoin de démarrer cette boucle d’événement en utilisant la fonction app.exec_().

Si nous ne le faisons pas, le programme va s’exécuter directement parce qu’il ne va pas continuer à tourner sur lui-même et cette boucle d’événement ici attend des événements de notre part pour s’exécuter là.

Python
 pythonCopybasicWindow()

Maintenant, nous allons mettre tout cela dans une fonction qui pourrait être appelée pour démarrer notre fenêtre en cours d’exécution.

Fenêtre de base de PyQt5

PyQt5 Changer la taille de la fenêtre

Si nous voulons changer la taille de la fenêtre, nous pouvons utiliser la méthode setGeometry() du widget de fenêtre.

Python
 pythonCopyimport sys
from PyQt5 import QtWidgets


def basicWindow():
    app = QtWidgets.QApplication(sys.argv)
    windowExample = QtWidgets.QWidget()
    windowExample.setGeometry(0, 0, 400, 400)
    windowExample.setWindowTitle("Basic Window Example")
    windowExample.show()
    sys.exit(app.exec_())


basicWindow()
Python
 pythonCopywindowExample.setGeometry(0, 0, 400, 400)

La méthode setGeometry() prend 4 entiers comme argument d’entrée qui sont

  • coordonnée X
  • Coordonnée Y
  • Largeur du cadre
  • Hauteur du cadre

Par conséquent, la taille de la fenêtre d’exemple est de 400 x 400 pixels.

Taille de la fenêtre PyQt5

PyQt5 Ajouter une icône de fenêtre

Python
 pythonCopyimport sys
from PyQt5 import QtWidgets, QtGui


def basicWindow():
    app = QtWidgets.QApplication(sys.argv)
    windowExample = QtWidgets.QWidget()
    windowExample.setWindowTitle("Basic Window Example")
    windowExample.setWindowIcon(QtGui.QIcon("python.jpg"))
    windowExample.show()
    sys.exit(app.exec_())


basicWindow()
Python
 pythonCopywindowExample.setWindowIcon(QtGui.QIcon("python.jpg"))

Il définit l’icône de la fenêtre comme étant python.jpg. Le paramètre de la méthode setWindowIcon est l’objet QIcon du module QtGui.

Taille de la fenêtre PyQt5

Vous aimez nos tutoriels ? Abonnez-vous à DelftStack sur YouTube pour nous aider à créer davantage de tutoriels vidéo de haute qualité. Abonnez-vous
Auteur: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook