Create Table Using PyQt5 in Python
PyQt5:
PyQt5 is one of several solutions that Python offers for creating GUI applications. Cross-platform GUI toolkit PyQt5 is a collection of Python interfaces for Qt version 5. With this library's capabilities and convenience, one can quickly create an interactive computer program. Front-end and Back-end make up a GUI application. To speed up development & allow more time to be spent on back-end tasks, PyQt5 has developed a tool called "QtDesigner" that allows one to drag and drop elements to create the front end. Riverbank Computing is the company in charge of developing and maintaining PyQt. The most recent stable version is PyQt6. The PyQt main version's release cycle matches Qt's, as per the release history. The PyQt codebase is a complex system with both C++ and Py code at its core. It is, therefore, more difficult to create and download it from sources than other Python libraries. Even more detailed installation instructions may be found in the instructions for the specific PyQt version we want to utilize. It comes with installation instructions for both the GPL and the paid versions. Before the window shows on the screen, a few essential concepts regarding the organization of applications in the Qt world must be explained. Unless you're already familiar with event loops, you can proceed to the next part without risk. The cornerstone of all Qt programs is the QApplication class. For each application to function, there must be a single QApplication object. The primary loop in your program that manages all interfaces with the GUI is housed in this object.
An incident is produced and stored in the action queue each time the user interacts using your program by pressing the keys, pressing the windows mouse, and moving the mouse. The queue is checked to see whether there are any awaiting events after each iteration of the event loop. The event is passed to the appropriate interrupt handler and controlled if one is found. The event handler then hands back control to the event loop so it can continue to wait for new events after handling the current one. There is just one open event loop per application.
Installation:
pip install pyqt5
pip install pyqt5-tools
Utilizing a QT designer tool to create a signup form. No coding is necessary to develop this skill, buttons, text boxes, etc.! It is a drag-and-drop kind of setting. Therefore, using PyQt is easier than Tkinter.
Introduction:
Utilizing a virtual environment is Python's most effective technique to control dependencies. To put it in simple terms, a local directory that includes the libraries needed for a specific project is a virtual environment. At the same time, all your projects would be impacted by a system-wide installation of those libraries. Every GUI program must have a single instance of QApplication; this is a requirement of the Qt framework. Several Qt components must be imported before they may be utilized. Thus, nearly any PyQt program you develop will require it.
CODE:
import sys
from PyQt5.QtWidgets import *
#The Main Window
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 - QTableWidget'
self.left = 0
self.top = 0
self.width = 300
self.height = 200
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.createTable()
self.layout = QVBoxLayout()
self.layout.addWidget(self.tableWidget)
self.setLayout(self.layout)
#Showing the window
self.show()
#Table creation using PyQt5
def createTable(self):
self.tableWidget = QTableWidget()
#count of row
self.tableWidget.setRowCount(4)
#count of column
self.tableWidget.setColumnCount(2)
self.tableWidget.setItem(0,0, QTableWidgetItem("Name"))
self.tableWidget.setItem(0,1, QTableWidgetItem("City"))
self.tableWidget.setItem(1,0, QTableWidgetItem(" "))
self.tableWidget.setItem(1,1, QTableWidgetItem(" "))
self.tableWidget.setItem(2,0, QTableWidgetItem(""))
self.tableWidget.setItem(2,1, QTableWidgetItem(" "))
self.tableWidget.setItem(3,0, QTableWidgetItem(" "))
self.tableWidget.setItem(3,1, QTableWidgetItem(" "))
self.tableWidget.setItem(4,0, QTableWidgetItem(" "))
self.tableWidget.setItem(4,1, QTableWidgetItem(" "))
self.tableWidget.setItem(5,0, QTableWidgetItem(" "))
self.tableWidget.setItem(5,1, QTableWidgetItem(" "))
self.tableWidget.setItem(6,0, QTableWidgetItem(" "))
self.tableWidget.setItem(6,1, QTableWidgetItem(" "))
self.tableWidget.setItem(7,0, QTableWidgetItem(" "))
self.tableWidget.setItem(7,1, QTableWidgetItem(" "))
self.tableWidget.setItem(8,0, QTableWidgetItem(" "))
self.tableWidget.setItem(8,1, QTableWidgetItem(" "))
self.tableWidget.setItem(9,0, QTableWidgetItem(" "))
self.tableWidget.setItem(9,1, QTableWidgetItem(" "))
self.tableWidget.setItem(10,0, QTableWidgetItem(" "))
self.tableWidget.setItem(10,1, QTableWidgetItem(" "))
#Table will fit the screen horizontally
self.tableWidget.horizontalHeader().setStretchLastSection(True)
self.tableWidget.horizontalHeader().setSectionResizeMode(
QHeaderView.Stretch)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
CODE EXPLANATION:
Importing sys and PyQt5 modules for the very first step. A table is a row-and-column data layout that is frequently used in data analysis, research, and communication. QTableWidget allows us to add one or more columns to our PyQt application. We'll use an instance where we wish to have a column in our application that lists the names of various persons along with their cities. We can retrieve the information from a sql or any other storage system. After importing the modules, we extract widgets from PyQt5 to build a title named “QTablewidget” with the dimensions from geometry left and the top is 0, and the width is equal to 300. The height is 200, so the output is displayed clearly on the user interface. Creating a layout with QVBoxLayout, then with the help of the table widget, we set a column and a row with a specific count and insert the items into the table using the item keyword.
OUTPUT:
