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
|
import sys
from PyQt5 import (QtWidgets, QtCore, QtSql)
from PyQt5.QtWidgets import QApplication, QComboBox, QMainWindow, QWidget
import sqlite3
class ComboBoxExample(QMainWindow):
def __init__(self):
super().__init__()
self.conn = sqlite3.connect("database.db")
cursor = self.conn.cursor()
QWidget.__init__(self)
self.setWindowTitle("client")
self.setMinimumHeight(450)
self.setMinimumWidth(700)
# Tableau client
cursor.execute("SELECT client.id, client.name, client.id_pays, pays.nom_pays, pays.nom_pays "
"FROM client INNER JOIN pays ON client.id_pays = pays.id")
result = cursor.fetchall()
# Liste des pays pour combobox
cursor.execute("SELECT * FROM pays")
self.values = [row[1] for row in cursor.fetchall()]
# affichage Tableau Client
self.tableau = QtWidgets.QTableWidget(self)
self.tableau.setGeometry(20, 100, 550, 300)
# définition du tableau Client
self.tableau.setRowCount(len(result))
self.tableau.setColumnCount(len(result[0]))
self.tableau.setHorizontalHeaderLabels(["id client", "client", "id pays", "pays", "pays"])
# Remplissage du tableau Client avec les données de la table
for i, row in enumerate(result):
for j, item in enumerate(row):
self.combo = QComboBox()
self.combo.addItems(self.values)
self.tableau.setCellWidget(i, 4, self.combo)
self.tableau.setItem(i, j, QtWidgets.QTableWidgetItem(str(item)))
# Affichage du tableau dans la fenêtre
self.tableau.show()
self.tableau.resizeColumnsToContents()
self.tableau.setColumnWidth(0, 100)
self.tableau.setColumnWidth(1, 100)
self.tableau.setColumnWidth(2, 100)
self.tableau.setColumnWidth(3, 100)
self.tableau.setColumnWidth(4, 100)
self.tableau.resizeRowsToContents()
app = QApplication(sys.argv)
combo_box_example = ComboBoxExample()
combo_box_example.show()
sys.exit(app.exec_()) |
Partager