How to Build a Simple GUI in Python and PyQt5

In this Python PyQt5 article we want to learn about How to Build a Simple GUI in Python and PyQt5, so PyQt5 is Python GUI Development library and you can build different types of Python GUI applications with PyQt5. to start building simple gui in pyqt5, first of all you need to install this library using pip, pip install PyQt5.

 

for building simple GUI in Python using PyQt5 you can follow these steps:

 

  1. Import necessary modules:

 

 

  1. Create an instance of QApplication class:

 

 

  1. Create an instance of QWidget class:

 

 

  1. Create an instance of the QLabel class and set its text:

 

 

  1. Create an instance of the QVBoxLayout class and set it as the layout for the window:

 

 

  1. Add label to the layout:

 

 

  1. Show the window:

 

 

  1. Start the event loop of the application:

 

 

This is the complete code

 

When you run this code simple window with label that says “Hello From Geekscoders.com” will appear.

You can customize the appearance of the label by setting different properties such as font, color, and alignment. for example to set the label font to bold and red you can do:

 

To set the label’s alignment to center, you can do:

 

 

You can also set the window’s title using the setWindowTitle method:

 

 

This is the complete code with Object Oriented Programming

In this example we have defined MyWindow class that extends from QWidget. we creates an instance of QLabel and set it as member of the class. after that we set up the layout for the window by creating an instance of QVBoxLayout and adding the label to it. we also set the window’s title and geometry.

and at the end we customize the appearance of the label by setting its stylesheet and alignment.

 

 

 

Run the complete code and this will be the result.

How to Build a Simple GUI in Python and PyQt5
How to Build a Simple GUI in Python and PyQt5

 

 

Learn More on Python

Leave a Comment