Using a library to create a GUI-based application

Now, we are actually going to use the PyQt5 library to create a simple GUI application. In this section, we are going to create a simple window. In that window, we will have one button and a label. After clicking on that button, some message will get printed in the label.

First, we will see how to create the button widget. The following line will create a button widget:

            b = QPushButton('Click', self)

Now, we will see how to create a label. The following line will create a label:

            l = QLabel(self)

Now, we will see how to create the button and label and how to perform an operation after clicking on that button. For that, create a print_message.py script and write following code in it:

import sys
from PyQt5.QtWidgets import QApplication, QLabel, QPushButton, QWidget
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtGui import QIcon

class simple_app(QWidget):
def __init__(self):
super().__init__()
self.title = 'Main app window'
self.left = 20
self.top = 20
self.height = 300
self.width = 400
self.app_initialize()

def app_initialize(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.height, self.width)
b = QPushButton('Click', self)
b.setToolTip('Click on the button !!')
b.move(100,70)
self.l = QLabel(self)
self.l.resize(100,50)
self.l.move(100,200)
b.clicked.connect(self.on_click)
self.show()

@pyqtSlot()
def on_click(self):
self.l.setText("Hello World")

if __name__ == '__main__':
appl = QApplication(sys.argv)
ex = simple_app()
sys.exit(appl.exec_())

Run the script and you will get the output as follows:

student@ubuntu:~/gui_example$ python3 print_message.py

 

In the preceding example, we imported the necessary PyQt5 modules. Then, we created the application. The QPushButton creates the widget and the first argument we entered is a text that will be printed on the button. Next, we have a QLabel widget on which we are printing a message, which will get printed when we will click on the button. Next, we created an on_click() function that will perform the printing operation after clicking on the button. The on_click() is the slot we created.

Now, we are going to see an example of the box layout. For that, create a box_layout.py script and write following code in it:

from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout

appl = QApplication([])
make_window = QWidget()
layout = QVBoxLayout()

layout.addWidget(QPushButton('Button 1'))
layout.addWidget(QPushButton('Button 2'))

make_window.setLayout(l)
make_window.show()

appl.exec_()

Run the script and you will get the following output:

student@ubuntu:~/gui_example$ python3 box_layout.py

In the preceding example, we created a box layout. In that we have placed two buttons. This script is just for explaining the box layout. l = QVBoxLayout() will create a box layout.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset