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:
- Import necessary modules:
1 2 |
import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout |
- Create an instance of QApplication class:
1 |
app = QApplication(sys.argv) |
- Create an instance of QWidget class:
1 |
window = QWidget() |
- Create an instance of the QLabel class and set its text:
1 |
label = QLabel('Hello From GeeksCoders.com') |
- Create an instance of the QVBoxLayout class and set it as the layout for the window:
1 2 |
layout = QVBoxLayout() window.setLayout(layout) |
- Add label to the layout:
1 |
layout.addWidget(label) |
- Show the window:
1 |
window.show() |
- Start the event loop of the application:
1 |
sys.exit(app.exec_()) |
This is the complete code
1 2 3 4 5 6 7 8 9 10 11 |
import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout app = QApplication(sys.argv) window = QWidget() label = QLabel('Hello From GeeksCoders.com') layout = QVBoxLayout() window.setLayout(layout) layout.addWidget(label) window.show() sys.exit(app.exec_()) |
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:
1 |
label.setStyleSheet('font-weight: bold; color: red') |
To set the label’s alignment to center, you can do:
1 |
label.setAlignment(QtCore.Qt.AlignCenter) |
You can also set the window’s title using the setWindowTitle method:
1 |
window.setWindowTitle('My Window') |
This is the complete code with Object Oriented Programming
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 |
import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout from PyQt5.QtCore import Qt class MyWindow(QWidget): def __init__(self): super().__init__() # Set the window properties self.setWindowTitle('GeeksCoders.com') self.setGeometry(100, 100, 300, 200) # Create a label self.label = QLabel('Hello From GeeksCoders.com!') # Customize the label self.label.setStyleSheet('font-weight: bold; color: red') self.label.setAlignment(Qt.AlignCenter) # Set the layout layout = QVBoxLayout() layout.addWidget(self.label) self.setLayout(layout) if __name__ == '__main__': app = QApplication(sys.argv) # Create a window instance window = MyWindow() # Show the window window.show() sys.exit(app.exec_()) |
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.
