PyQt5

PyQt5 – Window Widget

PyQt5 is a set of Python bindings for the cross-platform application framework that combines all the advantages of Qt and Python. With PyQt5, you can include Qt libraries in Python code, enabling you to write GUI applications in Python. In other words, PyQt allows you to access all the facilities provided by Qt through Python code. Since PyQt5 depends on the Qt libraries to run, when you install PyQt5, the required version of Qt is also installed automatically on your machine.

Before starting please ensure that you have installed PyQt5 or install it by running this command.

sudo pip3 install PyQt5

It will install PyQt5 libraries as well as the PyQt5-sip library.

Do Check our other tutorial for building an python gui app with Tkinter.


PyQt5 – Window

Every GUI application has a top-level widget and the rest of the widgets are called its children. The top-level widget can be QDialog , QWidget , or QMainWindow , depending on the template you require.

Related Post

In this tutorial we are using QWidget as our top level widget. Let’s start by first importing the libraries.

import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QWidget

Then we will make a Window class by inheriting QWidget class and then initialized the constructor with various parameters like height, width, top, title etc.

class Window(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 Window - pyblog.in'
        self.left = 20
        self.top = 20
        self.height = 600
        self.width = 800
        self.initUI()
        
    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.show()
    

Lastly we will create an object of the class and run it.

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec_())

Run using

python3 window.py

Output


How it worked??

  • It imports the necessary modules. QWidget is the base class of all user interface objects in PyQt5.
  • It creates a new Window class that inherits from the base class, QWidget.
  • It provides the default constructor for QWidget. The default constructor has no parent, and a widget with no parent is known as a window.
  • It creates an application object with the name app through the QApplication() method. Every PyQt5 application must create sys.argv application object which contains a list of arguments from the command line, and it is passed to the method while creating the application object. The sys.argv parameter helps in passing and controlling the startup attributes of a script.
  • An instance of the Window class is created with the name window.
  • The show() method will display the widget on the screen.
  • The sys.exit() method ensures a clean exit, releasing memory resources.

Please Note that, The exec_() method has an underscore because exec is a Python keyword.


Code

import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QWidget

class Window(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 Window - pyblog.in'
        self.left = 20
        self.top = 20
        self.height = 600
        self.width = 800
        self.initUI()
        
    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.show()
    
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Window()
    sys.exit(app.exec_())

K

Share

Recent Posts

  • Programming

Mastering Print Formatting in Python: A Comprehensive Guide

In Python, the print() function is a fundamental tool for displaying output. While printing simple…

10 months ago
  • Programming

Global Variables in Python: Understanding Usage and Best Practices

Python is a versatile programming language known for its simplicity and flexibility. When working on…

10 months ago
  • Programming

Secure Your Documents: Encrypting PDF Files Using Python

PDF (Portable Document Format) files are commonly used for sharing documents due to their consistent…

10 months ago
  • Programming

Creating and Modifying PDF Files in Python: A Comprehensive Guide with Code Examples

PDF (Portable Document Format) files are widely used for document exchange due to their consistent…

10 months ago
  • Programming

Boosting Python Performance with Cython: Optimizing Prime Number Detection

Python is a high-level programming language known for its simplicity and ease of use. However,…

10 months ago
  • Programming

Using OOP, Iterator, Generator, and Closure in Python to implement common design patterns

Object-Oriented Programming (OOP), iterators, generators, and closures are powerful concepts in Python that can be…

10 months ago

This website uses cookies.