In this Python PyQt5 lesson, we want to learn about Python PyQt5 QTableWidget, so using pyqt5 qtablewidget you can create tables in rows and columns.
In this lesson we are going to create two examples on pyqt QTableWidget class, the first example is using Qt Designer and the second one is using coding. you can just write pyqt5designer in your terminal, after opening the Qt Designer you need to create Widgets window. now we add widgets in Qt Designer.
- Add a Vertical Layout in the Qt Designer
- Add a TableWidget item in the VBoxLayout
So now you can add rows, columns and also items in your QTableWidget. right click on the tablewidget and click on the Edit Items, and from there you can add columns. so these are our columns.
These are rows for qtablewidget.
These are items for the qtablewidget.
Also you can add styles for the QTableWidget by right clicking on the tablewidget and choosing Change Stylesheet.
1 2 3 4 5 6 7 8 |
QTableWidget { background-color:rgb(255, 255, 0) } |
And this is our design.
After completing the design you need to save the .ui file, iam going to name it tablewidget.ui, now copy the file and paste it in the Scripts folder of your Python installation, because we want to convert our ui file in to python file and for converting you need to use pyuic5 module. pyuic5 module is located in the Scripts folder of your Python installation, run this command for converting in your terminal.
1 |
pyuic5 tablewidget.ui -o tablewidgetex.py -x |
And this is the converted code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# -*- coding: utf-8 -*- # Form implementation generated from # reading ui file 'tabelwidget.ui' # # Created by: PyQt5 UI code generator 5.15.1 # # WARNING: Any manual changes made to this # file will be lost when pyuic5 is # run again. Do not edit this file unless you # know what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(435, 313) self.verticalLayout_2 = QtWidgets.QVBoxLayout(Form) self.verticalLayout_2.setObjectName("verticalLayout_2") self.verticalLayout = QtWidgets.QVBoxLayout() self.verticalLayout.setObjectName("verticalLayout") self.tableWidget = QtWidgets.QTableWidget(Form) self.tableWidget.setStyleSheet("QTableWidget {\n" "\n" "background-color:rgb(255, 255, 0)\n" " \n" "\n" "\n" "\n" "}") self.tableWidget.setObjectName("tableWidget") self.tableWidget.setColumnCount(4) self.tableWidget.setRowCount(3) item = QtWidgets.QTableWidgetItem() brush = QtGui.QBrush(QtGui.QColor(0, 0, 0)) brush.setStyle(QtCore.Qt.NoBrush) item.setForeground(brush) self.tableWidget.setVerticalHeaderItem(0, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(1, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(2, item) item = QtWidgets.QTableWidgetItem() font = QtGui.QFont() font.setPointSize(12) font.setBold(True) font.setWeight(75) item.setFont(font) self.tableWidget.setHorizontalHeaderItem(0, item) item = QtWidgets.QTableWidgetItem() font = QtGui.QFont() font.setPointSize(12) font.setBold(True) font.setWeight(75) item.setFont(font) self.tableWidget.setHorizontalHeaderItem(1, item) item = QtWidgets.QTableWidgetItem() font = QtGui.QFont() font.setPointSize(12) font.setBold(True) font.setWeight(75) item.setFont(font) self.tableWidget.setHorizontalHeaderItem(2, item) item = QtWidgets.QTableWidgetItem() font = QtGui.QFont() font.setPointSize(12) font.setBold(True) font.setWeight(75) item.setFont(font) self.tableWidget.setHorizontalHeaderItem(3, item) item = QtWidgets.QTableWidgetItem() brush = QtGui.QBrush(QtGui.QColor(255, 0, 0)) brush.setStyle(QtCore.Qt.NoBrush) item.setForeground(brush) self.tableWidget.setItem(0, 0, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setItem(0, 1, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setItem(0, 2, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setItem(0, 3, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setItem(1, 0, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setItem(1, 1, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setItem(1, 2, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setItem(1, 3, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setItem(2, 0, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setItem(2, 1, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setItem(2, 2, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setItem(2, 3, item) self.verticalLayout.addWidget(self.tableWidget) self.verticalLayout_2.addLayout(self.verticalLayout) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "PyQt5 TableWidget")) item = self.tableWidget.verticalHeaderItem(0) item.setText(_translate("Form", "1")) item = self.tableWidget.verticalHeaderItem(1) item.setText(_translate("Form", "2")) item = self.tableWidget.verticalHeaderItem(2) item.setText(_translate("Form", "3")) item = self.tableWidget.horizontalHeaderItem(0) item.setText(_translate("Form", "Name")) item = self.tableWidget.horizontalHeaderItem(1) item.setText(_translate("Form", "Email")) item = self.tableWidget.horizontalHeaderItem(2) item.setText(_translate("Form", "Phone")) item = self.tableWidget.horizontalHeaderItem(3) item.setText(_translate("Form", "Address")) __sortingEnabled = self.tableWidget.isSortingEnabled() self.tableWidget.setSortingEnabled(False) item = self.tableWidget.item(0, 0) item.setText(_translate("Form", "Parwiz")) item = self.tableWidget.item(0, 1) item.setText(_translate("Form", "par@gmail.com")) item = self.tableWidget.item(0, 2) item.setText(_translate("Form", "5656565")) item = self.tableWidget.item(0, 3) item.setText(_translate("Form", "This is address")) item = self.tableWidget.item(1, 0) item.setText(_translate("Form", "John Doe")) item = self.tableWidget.item(1, 1) item.setText(_translate("Form", "john@gmail.com")) item = self.tableWidget.item(1, 2) item.setText(_translate("Form", "2323232")) item = self.tableWidget.item(1, 3) item.setText(_translate("Form", "This is address")) item = self.tableWidget.item(2, 0) item.setText(_translate("Form", "New Name")) item = self.tableWidget.item(2, 1) item.setText(_translate("Form", "new@gmail.com")) item = self.tableWidget.item(2, 2) item.setText(_translate("Form", "34342323")) item = self.tableWidget.item(2, 3) item.setText(_translate("Form", "New Address")) self.tableWidget.setSortingEnabled(__sortingEnabled) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec_()) |
So now run the complete code and this is result.
Creating PyQt QTableWidget with coding
OK now let’s create our pyqt5 tablewidget using coding, this is the complete source code for creating QTableWidget.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
from PyQt5.QtWidgets import QApplication, QWidget,\ QVBoxLayout, QTableWidgetItem, QTableWidget import sys from PyQt5.QtGui import QIcon class Window(QWidget): def __init__(self): super().__init__() self.setGeometry(200,200,400,200) self.setWindowTitle("Creating TableWidget") self.setWindowIcon(QIcon("python.png")) self.create_tables() def create_tables(self): vbox = QVBoxLayout() table_widget = QTableWidget() table_widget.setRowCount(3) table_widget.setColumnCount(3) table_widget.setItem(0, 0, QTableWidgetItem("Name")) table_widget.setItem(0, 1, QTableWidgetItem("Email")) table_widget.setItem(0, 2, QTableWidgetItem("Phone")) #adding items table_widget.setItem(1,0, QTableWidgetItem("Parwiz")) table_widget.setItem(1, 1, QTableWidgetItem("parwiz@gmail.com")) table_widget.setItem(1, 2, QTableWidgetItem("565656")) table_widget.setItem(2, 0, QTableWidgetItem("John")) table_widget.setItem(2, 1, QTableWidgetItem("john@gmail.com")) table_widget.setItem(2, 2, QTableWidgetItem("3232323")) vbox.addWidget(table_widget) self.setLayout(vbox) App = QApplication(sys.argv) window = Window() window.show() sys.exit(App.exec()) |
These are our window requirements like window title, window icon, width and height of the window, x and y position of the window.
1 2 3 |
self.setGeometry(200,200,400,200) self.setWindowTitle("Creating TableWidget") self.setWindowIcon(QIcon("python.png")) |
In here we have created our QTableWidget object also we need to set the row and column count for the tablewidget.
1 2 3 |
table_widget = QTableWidget() table_widget.setRowCount(3) table_widget.setColumnCount(3) |
And using QTableWidgeItem class you can add items to the pyqt tablewidget.
1 2 3 |
table_widget.setItem(0, 0, QTableWidgetItem("Name")) table_widget.setItem(0, 1, QTableWidgetItem("Email")) table_widget.setItem(0, 2, QTableWidgetItem("Phone")) |
Run the complete code and this is the result.