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.
1 2 |
import sys from PyQt5.QtWidgets import QApplication, QWidget |
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([]).
1 |
app = QApplication(sys.argv) |
In here we need to create the object of our QWidget, right now we are not going to talk more about this.
1 |
window = QWidget() |
After creating of the window you need to show your window.
1 |
window.show() |
And this is the starting point of our event loop.
1 |
app.exec_() |
This is the complete source code
1 2 3 4 5 6 7 8 9 10 11 12 |
import sys from PyQt5.QtWidgets import QApplication, QWidget #application object app = QApplication(sys.argv) window = QWidget() window.show() # Start the event loop. app.exec_() |
Run the code and this is the result.
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
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 |
from PyQt5.QtWidgets import QApplication, QWidget import sys from PyQt5.QtGui import QIcon class WindowExample(QWidget): def __init__(self): super().__init__() self.setGeometry(200,200, 400,300) self.setWindowTitle("Geekscoders.com") self.setWindowIcon(QIcon('python.png')) #self.setFixedHeight(400) #self.setFixedWidth(300) #self.setWindowOpacity(0.5) self.setStyleSheet('background-color:green') self.show() app = QApplication(sys.argv) window = WindowExample() sys.exit(app.exec_()) |
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.
1 |
self.setGeometry(200,200,400,300) |
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.
1 2 |
self.setWindowTitle("Geekscoders.com") self.setWindowIcon(QIcon("python.png")) |
This is used for adding opacity to the window, the value is between 0 and 1.
1 |
self.setWindowOpacity(0.5) |
So now run the code complete code and this will be the result.