Creating Main Window in Pyqt5
PyQt5 : Creating Main Window .
In this blog we will create our first window of PyQt5 and also try some basic methods for resize it or change it's title .
if you don't have any idea about PyQt5 then you can check introduction to PyQt5 .
.
Creating Our first Window :
# PyQt5 : Dynamic Coding # creating mainwindow . from PyQt5.QtWidgets import QApplication,QWidget import sys class Example(QWidget): def __init__(self): super().__init__() if __name__ == "__main__": app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
Lets understand the code .
at first we import the classes from pyqt5 and also import system module .
Note : PyQt5 is not built-in so please install it before use .
now we create a class of name Example and inherit Qwidget class into it now creating the constructor of Example and also calling the constructor of Qwidget .
at last we used a if condition and if the condition is true then a object of QApplication and Example is created and we use ex.show( ) method to show our GUI on the screen .
What is QApplication( ) :
Every PyQt5 application must create an application object. The
sys.argv
parameter is a list of arguments from a command line. Python scripts can be run from the shell. It is a way how we can control the startup of our scripts. if you dont want to run the code from shell or cmd you can use ' [ ] ' insted of sys.argvWhat is QWidget ?
The
QWidget
widget is the base class of all user interface objects in PyQt5. We provide the default constructor for QWidget
. The default constructor has no parent. A widget with no parent is called a window.OUTPUT :
The output of above code will look like .
Create window using QMainwindow
Any Qwidget without a parent ( containing widget ) is its own window . This is a fine way to create windows however There is also a specific Qmainwindow class. This class have few advantages over Qwidgets like it includes the use of toolbars , statusbar , Menu etc .
So lets code :
# PyQt5 : Dynamic Coding # creating mainwindow . from PyQt5.QtWidgets import QApplication,QMainWindow import sys class Example(QMainWindow): def __init__(self): super().__init__() if __name__ == "__main__": app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
The code gives us the output same as above window .
Changing title of window .
# PyQt5 : Dynamic Coding # changing title . from PyQt5.QtWidgets import QApplication,QMainWindow import sys class Example(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('Dynamic Coding') if __name__ == "__main__": app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
For changing title i use a predefined method of PyQt5 = setWindowTitle('string')
OUTPUT :
Set geometry and make it non resizable .
# PyQt5 : Dynamic Coding # changing geometry . from PyQt5.QtWidgets import QApplication,QMainWindow import sys class Example(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('Dynamic Coding') self.setGeometry(100,100,300,200) self.setFixedSize(300,200) if __name__ == "__main__": app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
for setting the geometry we use setGeometry methods which takes four arguments and then are .
- position from X axis
- position from Y axis
- width of GUI
- height of GUI
Adding Icon at mainwindow .
# PyQt5 : Dynamic Coding # adding icon . from PyQt5.QtWidgets import QApplication,QMainWindow from PyQt5 import QtGui import sys class Example(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('Dynamic Coding') self.setGeometry(100,100,300,200) self.setFixedSize(300,200) self.setWindowIcon(QtGui.QIcon('icon.png')) if __name__ == "__main__": app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
For adding icon we import another PyQt5 class QtGui and use it with a method named setWindowIcon and inside it call a QtGui method QIcon and pass the icon name .
Note : The icon size 12x12 is reccomended .
Check out our previous post on PyQt5 .
# -----------------------------------------------------------------------------------------------------------------------------------------------------#
No comments