×

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:

Create Table Using PyQt5 in Python

Related Topics

ER diagram of the Bank Management System in python

What is an ER diagram? The full form of the ER diagram is the Entity Relationship diagram. This diagram is used in database management systems to have a rough idea of...

3 minutes read.

Python program for perfect number

Python program for perfect number Before writing any program for a given problem, we have to understand the problem for which we are creating a solution program. So, let's understand what...

2 minutes read.

GUI to extract lyrics from a song Using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

4 minutes read.

Creating Tables using Python MySQL

In this article, we are going to learn how to create tables in databases using Python MySQL. Introduction to Tables: Generally, databases are used in order to store the information in the...

9 minutes read.

Python e-book free download

Python is a booming language these days. It has many applications for making code easier; it is also an open-source language. There are many sources to learn python. In this...

3 minutes read.

Python MySQL Delete Operation

Python MySQL Delete Operation: Like the update operation where we were updating required field from a SQL table, we can also delete an entry from the table which we have...

4 minutes read.

Python Project Ideas for Beginners

Python project ideas for beginners Advanced Python is an amazing platform to grow and achieve success as a developer in the future. Python is a programming language that can be very...

7 minutes read.

How to check the version of the Python Interpreter?

As we all know what an interpreter is, and how important it is. We should also be aware of the fact that it is important to have knowledge of the...

2 minutes read.

Python Static Method

Introduction to methods that are majorly used in Python: Class MethodsInstance MethodsStatic Methods Before going to the in-depth concept of Static Methods, let us briefly discuss Class Methods and Instance Methods. Class Methods The...

4 minutes read.

Python | Read csv using pandas.read_csv()

Python is an excellent language for performing information analysis, owing to the fantastic biological system of information-driven python packages. Pandas is one of those packages that make taking in and...

4 minutes read.

Python String

Python String Python string is considered as the most popular data type present in Python language. In Python, string data type is the collection of characters, letters, white spaces etc. written...

31 minutes read.

Best app for python

What is python? Python is a prevalent, general-purpose, and high-level programming language. Python Programming Language is appropriate for experienced programmers and also for beginners. Python programming language is being utilized by...

19 minutes read.

Sentence to python vector

Conversion of a Sentence to Vector in Python Before starting the tutorial, let’s just recap about the vector and the respective package that has to be imported in Python. Python Vector: Putting simply,...

3 minutes read.

Python String Variable

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

4 minutes read.

Python Argmin

Introduction The argmin function is defined as numpy.argmin(). This function returns the index of the minimum value or element from a Numpy array in a specific axis. An array is taken...

3 minutes read.

Unit Testing in Python

The process of testing whether a particular unit is working properly or not is called “UNIT TESTING”. A unit test will check small components in your application. The first and...

7 minutes read.

Python variance() function

Variance The variance is the average of the square deviations from the mean. The variance will measure the spread of the dataset from its mean or median value. The greater the...

4 minutes read.

Python Unit Test Cheat String

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Dictionary keys() method

Python Dictionary keys() method The dictionary.keys() method in Python returns a view object that displays a list of all the keys in the dictionary Syntax dictionary.keys() Parameter NA Return This method returns a view object that displays...

2 minutes read.

Difference between python list and tuple

In this tutorial, we will understand the key differences between python lists and tuples in python. Firstly, let us see the similarities between Lists and Tuples. Both are two data types...

4 minutes read.