Exercice 18
Reprendre l'exrcice précédent ( Exercice 17 ) En ajoutant cette fois-ci un objet QTableWidget nommé 'tableWidget' et une méthode nommée 'display_data()' permettant d'afficher les données de la table 'student' au sein de ce dernier comme le montre la figure suivante:
Solution
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem import sqlite3 class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(529, 377) self.lbl_name = QtWidgets.QLabel(Form) self.lbl_name.setGeometry(QtCore.QRect(40, 20, 121, 16)) font = QtGui.QFont() font.setPointSize(12) self.lbl_name.setFont(font) self.lbl_name.setObjectName("lbl_name") self.lbl_name_2 = QtWidgets.QLabel(Form) self.lbl_name_2.setGeometry(QtCore.QRect(40, 60, 121, 16)) font = QtGui.QFont() font.setPointSize(12) self.lbl_name_2.setFont(font) self.lbl_name_2.setObjectName("lbl_name_2") self.lbl_name_3 = QtWidgets.QLabel(Form) self.lbl_name_3.setGeometry(QtCore.QRect(40, 100, 121, 16)) font = QtGui.QFont() font.setPointSize(12) self.lbl_name_3.setFont(font) self.lbl_name_3.setObjectName("lbl_name_3") self.lineEdit_name = QtWidgets.QLineEdit(Form) self.lineEdit_name.setGeometry(QtCore.QRect(160, 20, 311, 20)) self.lineEdit_name.setObjectName("lineEdit_name") self.lineEdit_email = QtWidgets.QLineEdit(Form) self.lineEdit_email.setGeometry(QtCore.QRect(160, 60, 311, 20)) self.lineEdit_email.setObjectName("lineEdit_email") self.comboBox_section = QtWidgets.QComboBox(Form) self.comboBox_section.setGeometry(QtCore.QRect(160, 100, 311, 22)) self.comboBox_section.setObjectName("comboBox_section") self.comboBox_section.addItem("") self.comboBox_section.addItem("") self.comboBox_section.addItem("") self.comboBox_section.addItem("") self.btn_validate = QtWidgets.QPushButton(Form) # add action to QPushButton self.btn_validate.clicked.connect(self.insert_data) self.btn_validate.setGeometry(QtCore.QRect(160, 140, 311, 31)) font = QtGui.QFont() font.setPointSize(12) self.btn_validate.setFont(font) self.btn_validate.setObjectName("btn_validate") self.tableWidget = QtWidgets.QTableWidget(Form) self.tableWidget.setGeometry(QtCore.QRect(30, 190, 441, 171)) self.tableWidget.setFrameShape(QtWidgets.QFrame.Panel) self.tableWidget.setFrameShadow(QtWidgets.QFrame.Sunken) self.tableWidget.setObjectName("tableWidget") self.tableWidget.setColumnCount(4) self.tableWidget.setRowCount(5) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(0, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(1, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(2, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(3, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(4, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(0, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(1, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(2, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(3, item) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) self.display_data() def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.lbl_name.setText(_translate("Form", "Name")) self.lbl_name_2.setText(_translate("Form", "Email")) self.lbl_name_3.setText(_translate("Form", "Section")) self.comboBox_section.setItemText(0, _translate("Form", "Math")) self.comboBox_section.setItemText(1, _translate("Form", "Physique")) self.comboBox_section.setItemText(2, _translate("Form", "Informatique")) self.comboBox_section.setItemText(3, _translate("Form", "SVT")) self.btn_validate.setText(_translate("Form", "Insert Data")) 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.verticalHeaderItem(3) item.setText(_translate("Form", "4")) item = self.tableWidget.verticalHeaderItem(4) item.setText(_translate("Form", "5")) item = self.tableWidget.horizontalHeaderItem(0) item.setText(_translate("Form", "ID")) item = self.tableWidget.horizontalHeaderItem(1) item.setText(_translate("Form", "Name")) item = self.tableWidget.horizontalHeaderItem(2) item.setText(_translate("Form", "Email")) item = self.tableWidget.horizontalHeaderItem(3) item.setText(_translate("Form", "Section")) def insert_data(self): name = self.lineEdit_name.text() email = self.lineEdit_email.text() section = self.comboBox_section.currentText() try: # Connexion à la base de données SQLite3 conn = sqlite3.connect('database.db') cursor = conn.cursor() # Création de la table s'il n'existe pas cursor.execute(''' CREATE TABLE IF NOT EXISTS student ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, email TEXT, section TEXT ) ''') # Insertion des données dans la table cursor.execute('INSERT INTO student (name, email, section) VALUES (?, ?, ?)', (name, email, section)) # Validation de la transaction conn.commit() # Fermeture de la connexion conn.close() # Affichage d'un message de réussite QtWidgets.QMessageBox.information(Form, 'Success', 'Data inserted successfully.') except Exception as e: # En cas d'erreur, affichage d'un message d'erreur QtWidgets.QMessageBox.critical(Form, 'Error', f'Error inserting data: {str(e)}') self.display_data() # display data def display_data(self): # Connexion à la base de données SQLite3 conn = sqlite3.connect('database.db') self.cursor = conn.cursor() # Execute a query to retrieve data from the SQLite table self.cursor.execute('SELECT * FROM student') data = self.cursor.fetchall() # Set the number of rows and columns in the QTableWidget self.tableWidget.setRowCount(len(data)) self.tableWidget.setColumnCount(len(data[0])) # Populate the QTableWidget with data for row_index, row_data in enumerate(data): for col_index, col_data in enumerate(row_data): item = QTableWidgetItem(str(col_data)) self.tableWidget.setItem(row_index, col_index, item) def closeEvent(self, event): # Close the SQLite connection when the application is closed self.connection.close() 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_()) |
Younes Derfoufi
CRMEF OUJDA