In this article, we will learn how to add and work with a table in our PyQt5 application. A table is an arrangement of data in rows and columns and widely used in communication, research, and data analysis. We can add one or more tables in our PyQt application using QTableWidget.
For a better understanding of the concept, we will take an example where we want to display the name and the city of different people in a table in our application. We can extract the data from a database, JSON file, or any other storage platform.
Example:
import sys from PyQt5.QtWidgets import * #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) #Show window self.show() #Create table def createTable(self): self.tableWidget = QTableWidget() #Row count self.tableWidget.setRowCount(4) #Column count self.tableWidget.setColumnCount(2) self.tableWidget.setItem(0,0, QTableWidgetItem("Name")) self.tableWidget.setItem(0,1, QTableWidgetItem("City")) self.tableWidget.setItem(1,0, QTableWidgetItem("Aloysius")) self.tableWidget.setItem(1,1, QTableWidgetItem("Indore")) self.tableWidget.setItem(2,0, QTableWidgetItem("Alan")) self.tableWidget.setItem(2,1, QTableWidgetItem("Bhopal")) self.tableWidget.setItem(3,0, QTableWidgetItem("Arnavi")) self.tableWidget.setItem(3,1, QTableWidgetItem("Mandsaur")) #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_()) |
Output:

Recommended Posts:
- PyQt5 - Bbackground image to lineedit part of ComboBox when mouse hover
- PyQt5 - Background image to lineedit part of non-editable ComboBox when mouse hover
- PyQt5 QSpinBox – Setting object name
- PyQt5 QSpinBox – Showing it on lower level
- PyQt5 QCalendarWidget - Setting Border to the tool buttons
- PyQt5 QCalendarWidget - Clicked signal
- PyQt5 QCalendarWidget - Removing memory reference
- PyQt5 QCalendarWidget - Setting Border to the months menu
- PyQt5 - Getting the role of selected item in ComboBox
- PyQt5 ComboBox - User entered items store at top
- PyQt5 ComboBox – User entered items not stored in drop down menu
- PyQt5 - How make duplicates insertions in ComboBox
- PyQt5 - Checking if the duplicate values are allowed or not in ComboBox
- PyQt5 - How know the insertion policy of the ComboBox
- PyQt5 – How to hide label | label.setHidden method
- PyQt5 QSpinBox - Ensuring Polished
- PyQt5 QSpinBox - Getting depth
- PyQt5 QSpinBox - Getting Pixel ratio
- PyQt5 QSpinBox - Getting System Identifier
- PyQt5 QLabel - Checking if the opacity effect is window type
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

