import sqlite3
import tkinter as tk
from tkinter import ttk, messagebox
# Connexion à la base de données
conn = sqlite3.connect("stock.db")
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS stock (
id INTEGER PRIMARY KEY AUTOINCREMENT,
nom TEXT NOT NULL,
quantite INTEGER NOT NULL CHECK(quantite >= 0),
reference TEXT NOT NULL,
sous_reference TEXT NOT NULL
)
""")
conn.commit()
# Fonction pour ajouter un produit
def ajouter_produit():
nom = entry_nom.get().strip()
quantite = entry_quantite.get().strip()
reference = entry_reference.get().strip()
sous_reference = entry_sous_reference.get().strip()
if not nom or not quantite or not reference or not sous_reference:
messagebox.showerror("Erreur", "Tous les champs sont obligatoires.")
return
if not quantite.isdigit() or int(quantite) < 0:
messagebox.showerror("Erreur", "La quantité doit être un nombre positif.")
return
try:
cursor.execute("INSERT INTO stock (nom, quantite, reference, sous_reference) VALUES (?, ?, ?, ?)",
(nom, int(quantite), reference, sous_reference))
conn.commit()
afficher_produits()
entry_nom.delete(0, tk.END)
entry_quantite.delete(0, tk.END)
entry_reference.delete(0, tk.END)
entry_sous_reference.delete(0, tk.END)
except sqlite3.Error as e:
messagebox.showerror("Erreur", f"Problème avec la base de données : {e}")
# Fonction pour afficher les produits
def afficher_produits():
for row in tree.get_children():
tree.delete(row)
cursor.execute("SELECT * FROM stock")
for row in cursor.fetchall():
tree.insert("", tk.END, values=row)
# Fonction pour supprimer un ou plusieurs produits sélectionnés
def supprimer_produit():
selected_items = tree.selection()
if not selected_items:
messagebox.showerror("Erreur", "Veuillez sélectionner au moins un produit à supprimer.")
return
confirmation = messagebox.askyesno("Confirmation", "Voulez-vous vraiment supprimer le(s) produit(s) sélectionné(s) ?")
if confirmation:
try:
for item in selected_items:
produit_id = tree.item(item)["values"][0]
cursor.execute("DELETE FROM stock WHERE id = ?", (produit_id,))
conn.commit()
afficher_produits()
except sqlite3.Error as e:
messagebox.showerror("Erreur", f"Problème avec la base de données : {e}")
# Interface graphique
root = tk.Tk()
root.title("Gestion de Stock")
root.geometry("650x400")
frame = tk.Frame(root)
frame.pack(pady=10)
# Champs de saisie
tk.Label(frame, text="Nom:").grid(row=0, column=0, sticky="w")
entry_nom = tk.Entry(frame)
entry_nom.grid(row=0, column=1, padx=5, pady=2)
tk.Label(frame, text="Quantité:").grid(row=1, column=0, sticky="w")
entry_quantite = tk.Entry(frame)
entry_quantite.grid(row=1, column=1, padx=5, pady=2)
tk.Label(frame, text="Référence:").grid(row=2, column=0, sticky="w")
entry_reference = tk.Entry(frame)
entry_reference.grid(row=2, column=1, padx=5, pady=2)
tk.Label(frame, text="Sous-référence:").grid(row=3, column=0, sticky="w")
entry_sous_reference = tk.Entry(frame)
entry_sous_reference.grid(row=3, column=1, padx=5, pady=2)
# Boutons
btn_frame = tk.Frame(frame)
btn_frame.grid(row=4, column=0, columnspan=2, pady=10)
tk.Button(btn_frame, text="Ajouter", command=ajouter_produit).pack(side=tk.LEFT, padx=5)
tk.Button(btn_frame, text="Supprimer", command=supprimer_produit).pack(side=tk.LEFT, padx=5)
# Tableau d'affichage
tree = ttk.Treeview(root, columns=("ID", "Nom", "Quantité", "Référence", "Sous-référence"), show="headings")
tree.heading("ID", text="ID")
tree.heading("Nom", text="Nom")
tree.heading("Quantité", text="Quantité")
tree.heading("Référence", text="Référence")
tree.heading("Sous-référence", text="Sous-référence")
for col in ("ID", "Nom", "Quantité", "Référence", "Sous-référence"):
tree.column(col, width=120)
tree.pack(expand=True, fill=tk.BOTH)
# Chargement initial des produits
afficher_produits()
# Gestion de la fermeture propre
def fermer_application():
conn.close()
root.destroy()
root.protocol("WM_DELETE_WINDOW", fermer_application)
root.mainloop()
To embed this program on your website, copy the following code and paste it into your website's HTML: