Exercice 16
Reprendre l'exercice précédent ( Exercice 15 ) en adoptant l'approche objet POO en Python:
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 |
from PyQt5.QtWidgets import QApplication, QWidget, QTableWidget, QTableWidgetItem import sys class Tablestudent(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle("Student Data") self.setGeometry(100, 100, 600, 400) # create a QTableWidget self.table = QTableWidget(self) self.table.setRowCount(5) self.table.setColumnCount(4) # Add header headerH = ['ID', 'Name', 'Email', 'Section'] self.table.setHorizontalHeaderLabels(headerH) # Adjust column widths self.table.setColumnWidth(0, 50) # Colonne ID self.table.setColumnWidth(1, 150) # Colonne Name self.table.setColumnWidth(2, 200) # Colonne email self.table.setColumnWidth(3, 150) # Colonne section # define the # définir les données des étudiants students_data = [ {'id': 1, 'name': 'Rachid', 'email': 'rachid@gmail.com', 'section': 'mathématique'}, {'id': 2, 'name': 'Nadia', 'email': 'nadia@gmail.com', 'section': 'informatique'}, {'id': 3, 'name': 'Nathalie', 'email': 'nathalie@gmail.com', 'section': 'svt'}, {'id': 4, 'name': 'Alberto', 'email': 'alberto@gmail.com', 'section': 'HG'}, {'id': 5, 'name': 'Farid', 'email': 'farid@gmail.com', 'section': 'math'} ] # initializ aow index row_index = 0 # Insert data for data in students_data: self.table.setItem(row_index, 0, QTableWidgetItem(str(data['id']))) self.table.setItem(row_index, 1, QTableWidgetItem(data['name'])) self.table.setItem(row_index, 2, QTableWidgetItem(data['email'])) self.table.setItem(row_index, 3, QTableWidgetItem(data['section'])) row_index +=1 self.table.setGeometry(50, 50, 450, 200) if __name__ == '__main__': app = QApplication([]) studentData = Tablestudent() studentData.show() sys.exit(app.exec_()) |
Younes Derfoufi
CRMEF OUJDA