×

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

Django vs NodeJS

Django and NodeJS are open-source frameworks used to develop web applications. Both Django and NodeJS are cross-platform and robust technology used for developing versatile web applications and mobile applications. Django Django is...

2 minutes read.

Python Set intersection_update() method

Python Set intersection_update() method The set.intersection_update() method in Python removes the items that is not present in both sets. It is different from the set.intersection() method, because the intersection()method returns a new set, with only  the common elements...

2 minutes read.

Anaconda python 3 installation for windows 10

If you are a problem solver and like to solve programming questions, the anaconda distribution for python could be one of the best options to use and solve problems in...

3 minutes read.

Python Dictionary get() method

Python Dictionary get() method The dictionary.get() method returns the value of the item with the specified key. Syntax dictionary.get(keyname, value) Parameter keyname- This parameter represents the key to be searched in the dictionary. value- The parameter...

1 minute read.

Python enumerate() function

Python enumerate() function The enumerate() function in Python takes a collection (e.g. a tuple) and returns it as an enumerate object. Syntax: enumerate(iterable, start=0) Parameter Iterable:  This parameter represents an iterable object start:    This parameter represents a number defining...

1 minute read.

CSV Module In Python

Introduction CSV stands for "comma separated values". It is the most common and simple file structure used for storing and arranging tabular data. for example; a spreadsheet or database. It stores...

5 minutes read.

How to build an Auto Clicker using Python?

What is an Auto Clicker? An Auto Clicker is software that is used for automating the click of a mouse on any particular element on the computer screen and controlling the...

8 minutes read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.

How to create a DataFrames in Python

How to create a DataFrames in Python Data Frame is a data structure in which data is stored in tabular form. They can also be referred as two-dimensional collection of data. Various...

5 minutes read.

List Index out of Range Python for Loop

The List is generally used in Python to store multiple items or elements inside one single variable. The List is ordered in nature, the List can contain the elements inside...

3 minutes read.

Sys Module in Python

What are Modules? The Modules are the kind of files that contain Python statements and definitions. The module is known by the name of the file followed by the suffix “.py”....

5 minutes read.

PyPi TensorFlow

It is a free open-source library for high-performative numerical computations. The architecture of TensorFlow allows us for easy deployment and computing across a varied number of platforms and from desktops...

3 minutes read.

Python Multithreading

In python, we can implement multithreading. In this tutorial, we will understand the basics of implementing multithreading in a python programming language. Multithreading is just like multiprocessing. We use it...

4 minutes read.

os.rename() method in Python

In Python, the os module provides the capacity to interact with the operating system. The operating system comes under the Python module. In this module, Python provides a specific feature...

3 minutes read.

How to Install Matplotlib in Python?

How to Install Matplotlib in Python The speed at which the enormous amount of data is generating has become a huge aid in understanding what's going on currently in the market....

4 minutes read.

Python round() function

Python round() function The round() function in Python returns a number rounded to ‘ndigits’ precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input. Syntax round(number[, ndigits]) Parameter Number: This parameter represents the...

1 minute read.

Python MongoDB Tutorial

An Introduction to MongoDB MongoDB is a document-oriented database application. It is an Open-source and platform-independent program. MongoDB is similar to few NoSQL databases that store the data in the documents...

17 minutes read.

Python System Command

To execute a program in Python, we need to execute some shell commands to run our program on the computer. Python will provide some shell commands in our background to...

3 minutes read.

Python dir() function

Python dir() function The dir() function in Python returns all properties and methods of the specified object, without the values. Syntax dir([object]) Parameter object: This parameter represents the object one wants to see the valid attributes. Return This function...

2 minutes read.

Python range() function

Python range() function The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Syntax range(stop)        or range(start, stop[, step]) Parameter start: It is...

1 minute read.