Python PyQt5 basic application with widgets

Python PyQt Basic: Exercise-2 with Solution

Write a Python program to create a basic PyQt application that opens an empty window with a specified size and title. Add various widgets (e.g. label, push button) to the blank window. Widgets should display text or perform a simple action.

From doc.qt.io:

QApplication Class: The QApplication class manages the GUI application's control flow and main settings.

QMainWindow Class: The QMainWindow class provides a main application window.

QLabel Class: The QLabel widget provides a text or image display.

QPushButton: The push button, or command button, is perhaps the most commonly used widget in any graphical user interface. Push (click) a button to command the computer to perform some action, or to answer a question. Typical buttons are OK, Apply, Cancel, Close, Yes, No and Help.

QVBoxLayout Class: The QVBoxLayout class lines up widgets vertically.

QWidget: The QWidget class is the base class of all user interface objects.

Sample Solution:

Python Code:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QVBoxLayout, QWidget

def main():
    # Create a PyQt application
    app = QApplication(sys.argv)

    # Create a QMainWindow (main window)
    main_window = QMainWindow()
    # Set the window properties (title and initial size)
    main_window.setWindowTitle("Basic PyQt Application")
    main_window.setGeometry(100, 100, 400, 300)  # (x, y, width, height)
    # Create a central widget for the main window
    central_widget = QWidget()
    # Create widgets (QLabel and QPushButton)
    label = QLabel("Example of PyQt label!")
    button = QPushButton("Example of PyQt pushbutton!")
    # Create a layout to arrange the widgets vertically
    layout = QVBoxLayout()
    # Set the layout for the central widget
    # Show the window
    # Run the application's event loop
if __name__ == "__main__":


In the exercise above -

  • Import the necessary modules from PyQt5.
  • Create a "QApplication" object to manage application control flow and settings.
  • Create a "QMainWindow" object for the main application window and set its title and initial size.
  • Create a central widget (a QWidget) to hold our other widgets.
  • Create two widgets: a QLabel displaying "Example of PyQt label!" and a QPushButton with the label "Example of PyQt pushbutton!".
  • Create a vertical layout (QVBoxLayout) and add 'QLabel' and 'QPushButton' to it.
  • Set the layout for the central widget so that it arranges the widgets vertically.
  • Finally, start the application's event loop with app.exec_() to handle user interactions and events.


PyQt: Python PyQt5 basic application with widgets. Part-1


Flowchart: Python PyQt5 basic application with widgets.

Python Code Editor:

Previous: Python PyQt5 blank window example.
Next: Python PyQt5 window with multiple widgets and layouts.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Summing a sequence of numbers (calculating the sum of zero to ten with skips):

>>> l = range(0,10,2)
>>> sum(l)


We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook