0% found this document useful (0 votes)
18 views5 pages

New Text Document

Uploaded by

kaws doom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

New Text Document

Uploaded by

kaws doom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

import sqlite3

import tkinter as tk
from tkinter import messagebox, ttk

# Function to initialize the database and create tables


def initialize_db():
connexion = sqlite3.connect('gestion_etudiants.db')
curseur = connexion.cursor()

# Creating tables if they do not exist


curseur.execute('''CREATE TABLE IF NOT EXISTS etudiants (
id_etudiant INTEGER PRIMARY KEY AUTOINCREMENT,
nom TEXT,
prenom TEXT,
date_naissance DATE,
email TEXT
)''')

curseur.execute('''CREATE TABLE IF NOT EXISTS modules (


id_module INTEGER PRIMARY KEY AUTOINCREMENT,
nom_module TEXT,
description TEXT
)''')

curseur.execute('''CREATE TABLE IF NOT EXISTS inscription (


id_inscription INTEGER PRIMARY KEY AUTOINCREMENT,
id_etudiant INTEGER,
id_module INTEGER,
note REAL,
FOREIGN KEY (id_etudiant) REFERENCES etudiants(id_etudiant),
FOREIGN KEY (id_module) REFERENCES modules(id_module),
UNIQUE (id_etudiant, id_module) -- Ensures unique constraint
)''')

# Insert initial modules if they don't exist


modules_data = [
('Mathématiques', 'Calcul et algèbre de base'),
('Informatique', 'Introduction aux bases de données'),
('Physique', 'Mécanique et thermodynamique')
]

curseur.executemany('INSERT OR IGNORE INTO modules (nom_module, description)


VALUES (?, ?)', modules_data)

connexion.commit()
connexion.close()

# Function to add a student


def add_student():
nom = entry_nom.get()
prenom = entry_prenom.get()
date_naissance = entry_date_naissance.get()
email = entry_email.get()

# Validate input
if not (nom and prenom and date_naissance and email):
messagebox.showerror("Erreur", "Tous les champs doivent être remplis.")
return
# Insert student into the database
connexion = sqlite3.connect('gestion_etudiants.db')
curseur = connexion.cursor()

curseur.execute('INSERT INTO etudiants (nom, prenom, date_naissance, email)


VALUES (?, ?, ?, ?)',
(nom, prenom, date_naissance, email))

connexion.commit()
messagebox.showinfo("Succès", "Étudiant ajouté avec succès.")
clear_student_entries()

# Refresh the student list


refresh_student_list()

connexion.close()

# Function to clear student input fields


def clear_student_entries():
entry_nom.delete(0, tk.END)
entry_prenom.delete(0, tk.END)
entry_date_naissance.delete(0, tk.END)
entry_email.delete(0, tk.END)

# Function to assign or update grades for a student


def assign_grade():
selected_student = student_combobox.get()
note_physics = entry_note_physics.get()
note_math = entry_note_math.get()
note_cs = entry_note_cs.get()

# Validate input
if selected_student == "Sélectionner un étudiant":
messagebox.showerror("Erreur", "Sélectionnez un étudiant.")
return

# Extract student ID
student_id = [student[0] for student in students if f"{student[1]}
{student[2]}" == selected_student][0]

# Connect to the database


connexion = sqlite3.connect('gestion_etudiants.db')
curseur = connexion.cursor()

# Update or insert grades for each module if a note is provided


if note_physics:
try:
curseur.execute('''
INSERT INTO inscription (id_etudiant, id_module, note)
VALUES (?, (SELECT id_module FROM modules WHERE nom_module =
'Physique'), ?)
ON CONFLICT(id_etudiant, id_module)
DO UPDATE SET note = excluded.note
''', (student_id, float(note_physics)))
except ValueError:
messagebox.showerror("Erreur", "La note doit être un nombre.")
connexion.close()
return
if note_math:
try:
curseur.execute('''
INSERT INTO inscription (id_etudiant, id_module, note)
VALUES (?, (SELECT id_module FROM modules WHERE nom_module =
'Mathématiques'), ?)
ON CONFLICT(id_etudiant, id_module)
DO UPDATE SET note = excluded.note
''', (student_id, float(note_math)))
except ValueError:
messagebox.showerror("Erreur", "La note doit être un nombre.")
connexion.close()
return

if note_cs:
try:
curseur.execute('''
INSERT INTO inscription (id_etudiant, id_module, note)
VALUES (?, (SELECT id_module FROM modules WHERE nom_module =
'Informatique'), ?)
ON CONFLICT(id_etudiant, id_module)
DO UPDATE SET note = excluded.note
''', (student_id, float(note_cs)))
except ValueError:
messagebox.showerror("Erreur", "La note doit être un nombre.")
connexion.close()
return

connexion.commit()
messagebox.showinfo("Succès", "Notes assignées avec succès.")
clear_grade_entries()

# Refresh the grades display


display_grades()

connexion.close()

# Function to clear grade input fields


def clear_grade_entries():
entry_note_physics.delete(0, tk.END)
entry_note_math.delete(0, tk.END)
entry_note_cs.delete(0, tk.END)

# Function to refresh the student list


def refresh_student_list():
global students
connexion = sqlite3.connect('gestion_etudiants.db')
curseur = connexion.cursor()
curseur.execute('SELECT * FROM etudiants')
students = curseur.fetchall()
student_names = [f"{student[1]} {student[2]}" for student in students]

# Update the combobox with the new list of students


student_combobox['values'] = ["Sélectionner un étudiant"] + student_names
student_combobox.current(0) # Reset selection to the first option

connexion.close()

# Function to display student grades


def display_grades():
for i in tree.get_children():
tree.delete(i)

connexion = sqlite3.connect('gestion_etudiants.db')
curseur = connexion.cursor()

curseur.execute('''SELECT
e.nom AS nom_etudiant,
e.prenom AS prenom_etudiant,
MAX(CASE WHEN m.nom_module = 'Physique' THEN i.note ELSE NULL END) AS
Physique,
MAX(CASE WHEN m.nom_module = 'Mathématiques' THEN i.note ELSE NULL END) AS
Mathématiques,
MAX(CASE WHEN m.nom_module = 'Informatique' THEN i.note ELSE NULL END) AS
Informatique,
AVG(i.note) AS Moyenne_Generale
FROM
etudiants e
LEFT JOIN
inscription i ON e.id_etudiant = i.id_etudiant
LEFT JOIN
modules m ON i.id_module = m.id_module
GROUP BY
e.id_etudiant;''')

resultats = curseur.fetchall()
for ligne in resultats:
tree.insert("", tk.END, values=ligne)

connexion.close()

# Initialize database
initialize_db()

# Create the main window


root = tk.Tk()
root.title("Gestion des Étudiants")

# Create a frame for adding students


frame_add_student = tk.Frame(root)
frame_add_student.pack(pady=10)

tk.Label(frame_add_student, text="Nom:").grid(row=0, column=0)


entry_nom = tk.Entry(frame_add_student)
entry_nom.grid(row=0, column=1)

tk.Label(frame_add_student, text="Prénom:").grid(row=1, column=0)


entry_prenom = tk.Entry(frame_add_student)
entry_prenom.grid(row=1, column=1)

tk.Label(frame_add_student, text="Date de Naissance:").grid(row=2, column=0)


entry_date_naissance = tk.Entry(frame_add_student)
entry_date_naissance.grid(row=2, column=1)

tk.Label(frame_add_student, text="Email:").grid(row=3, column=0)


entry_email = tk.Entry(frame_add_student)
entry_email.grid(row=3, column=1)
tk.Button(frame_add_student, text="Ajouter Étudiant",
command=add_student).grid(row=4, columnspan=2)

# Create a frame for assigning grades


frame_assign_grade = tk.Frame(root)
frame_assign_grade.pack(pady=10)

tk.Label(frame_assign_grade, text="Sélectionner un étudiant:").grid(row=0,


column=0)
students = []
connexion = sqlite3.connect('gestion_etudiants.db')
curseur = connexion.cursor()
curseur.execute('SELECT * FROM etudiants')
students = curseur.fetchall()
student_names = [f"{student[1]} {student[2]}" for student in students]

student_combobox = ttk.Combobox(frame_assign_grade, values=["Sélectionner un


étudiant"] + student_names)
student_combobox.grid(row=0, column=1)

tk.Label(frame_assign_grade, text="Note Physique:").grid(row=1, column=0)


entry_note_physics = tk.Entry(frame_assign_grade)
entry_note_physics.grid(row=1, column=1)

tk.Label(frame_assign_grade, text="Note Mathématiques:").grid(row=2, column=0)


entry_note_math = tk.Entry(frame_assign_grade)
entry_note_math.grid(row=2, column=1)

tk.Label(frame_assign_grade, text="Note Informatique:").grid(row=3, column=0)


entry_note_cs = tk.Entry(frame_assign_grade)
entry_note_cs.grid(row=3, column=1)

tk.Button(frame_assign_grade, text="Attribuer Notes",


command=assign_grade).grid(row=4, columnspan=2)

# Create a frame for displaying grades


frame_display_grades = tk.Frame(root)
frame_display_grades.pack(pady=10)

tree = ttk.Treeview(frame_display_grades, columns=("Nom", "Prénom", "Physique",


"Mathématiques", "Informatique", "Moyenne Générale"), show='headings')
tree.heading("Nom", text="Nom")
tree.heading("Prénom", text="Prénom")
tree.heading("Physique", text="Physique")
tree.heading("Mathématiques", text="Mathématiques")
tree.heading("Informatique", text="Informatique")
tree.heading("Moyenne Générale", text="Moyenne Générale")
tree.pack()

# Display grades on startup


display_grades()

# Refresh student list on startup


refresh_student_list()

root.mainloop()

You might also like