PyQt5 Tutorials
About Lesson

This is our first PyQt5 lesson, in this lesson we want to talk about Installation & Create Window in PyQt5, so PyQt5 is a binding for Qt5 C++ a GUI framework for C++ programming language.

 

What is PyQt5 ?

PyQt5 is used to write all kinds of GUI applications, from accounting applications, to visualization tools used by scientists and engineers. it is possible to write PyQt5 applications that are just tens of lines long, and medium-size projects of 1000 to 10000 lines are very common. PyQt5 can be used free of charge for noncommercial purposes, but the license used by Python is different from that used by PyQt5 and Qt. Python is available with a very liberal license  that allows it to be used to development both commercial and noncommercial applications. Both  PyQt and Qt are dual licensed, this essentially allows them to be used to develop noncommercial  applications, which must in turn be licensed using an acceptable open source license such as the  GNU General Public License (GPL); or to be used to develop commercial applications in this case, a  commercial PyQt license and a commercial Qt license must be purchased.

 

 

OK now let’s create our first window in PyQt5, first we need some imports. basically we have imported some classes that we want to use. 

 

 

In every PyQt5 application we need to create the object of QApplication, and the sys.argv allows command line arguments for our application. if you don’t want to use command line arguments than you can leave it blank like this QApplication([]).

 

 

In here we need to create the object of our QWidget, right now we are not going to talk more about this.

 

 

After creating of the window you need to show your window.

 

 

And this is the starting point of our event loop.

 

This  is the complete source code

 

 

 

Run the code and this is the result.

PyQt5 Creating Window
PyQt5 Creating Window

 

 

Ok now we are going to add more functionality to our code and we want to use Object Oriented terminology for creating of our Window in PyQt5.

 

This is the complete source code for PyQt5 Installation & Create Window in PyQt5

 

 

You can see in the above code we have created a class that extends from QWidget, now there are three types of window class that you can use. 

  • QWidget: The QWidget class is the base class of all user interface
    objects, The widget is the important point of the user interface:
    it receives mouse, keyboard and other events from the window
    system, and paints a representation of itself on the screen.
  • QDialog: The QDialog class is the base class of dialog window
    and A dialog window is a top-level window mostly used for
    short-term tasks and brief communications with the user.
    QDialogs may be modal or modeless.
  • QMainWindow: The QMainWindow class provides a main application window
    A main window provides a framework for building an
    application’s user interface. PyQt5 has QMainWindow and its
    related classes for main window management. QMainWindow has
    its own layout to which you can add QToolBars, QDockWidgets,
    a QMenuBar, and a QStatusBar.

 

 

__init__” is a reserved method in python classes. It is called as a constructor in object oriented programming. This method is called when an object is created from a class and it allows the class to initialize the attributes of the class. The super() function is used to give access to methods and properties of a parent class. And the super() function makes class inheritance more manageable. The word ‘self’ is used to represent the instance of the class. By using the “self” keyword we can access the attributes and methods of the class in python.

 

This is used for setting the geometry of the window, the x,y position, width and height of the window.

 

 

In here you can set the title and icon for the window, make sure that you have already added an icon in your working directory.

 

 

This is used for adding opacity to the window, the value is between 0 and 1.

 

 

 

So now run the code complete code and this will be the result.

PyQt5 Installation & Create Window in PyQt5
PyQt5 Installation & Create Window in PyQt5