bonjour Isaac,
j’ai fait des tentatives au point de plus rien comprendre.
je vous demande de l’aide.
je joint le script complet
#!/usr/bin/python -> page-de fichier etudiant.py python
# -*- coding: utf-8 -*- -> formulaire d'inscription adhérent sqlite3
# Python 3.10.2 -> jacky michaud
import tkinter as tk
from tkinter import *
from tkinter import messagebox
from tkinter import ttk, messagebox
from tkinter.messagebox import *
import sqlite3
import sys,io,os
class Etudiant:
def __init__(self, root):
self.root = root
self.root.title("Description")
self.root.geometry("1510x775+5+5")
#########################
# formulaire haut
Gestion_Frame = Frame(self.root, bd=5, relief=GROOVE, bg="cyan")
Gestion_Frame.place(x=7, y=10, width=1495, height=280)
# les variables
self.id = StringVar() # id
self.titre = StringVar() # Titre
self.nom = StringVar() # Nom
self.prenom = StringVar() # Prenom
self.mail = StringVar() # Mail
self.telephone = StringVar() # Telephone
self.date = StringVar() # Date
self.cotisation = StringVar() # cotisation
self.ville = StringVar() # ville
self.code_postal = StringVar() # code_postal
self.impression = StringVar()
self.impression = ""
# self.Adresse_txt = StringVar()
self.recherch_par = StringVar()
self.recherch = StringVar()
gestion_title = Label(Gestion_Frame, text="Information de l'Adhérent", font=("times new roman", 30, "bold"), bg="cyan")
gestion_title.place(x=5, y=0)
# ID Ordinateur # id
id = Label(Gestion_Frame, text="ID adhérent : ", font=("times new roman", 15), bg="cyan")
id.place(x=5, y=50, width=180)
id_txt = Entry(Gestion_Frame, textvariable=self.id, font=("times new roman", 15), bg="lightgray")
id_txt.place(x=150, y=50, width=300)
id_txt.focus_set()
# Titre
titre = Label(Gestion_Frame, text="Titre : ", font=("times new roman", 15), bg="cyan")
titre.place(x=5, y=80, width=232)
titre_txt = ttk.Combobox(Gestion_Frame, textvariable=self.titre, font=("times new roman", 15), state="readonly")
titre_txt["values"]=(" Mr", " Mme", " Mlle")
titre_txt.place(x=150, y=80,width=100)
# Nom
nom = Label(Gestion_Frame, text="Nom : ", font=("times new roman", 15), bg="cyan")
nom.place(x=5, y=110, width=230)
nom_txt = Entry(Gestion_Frame, textvariable=self.nom, font=("times new roman", 15), bg="lightgray")
nom_txt.place(x=150, y=110, width=300)
# prenom
prenom = Label(Gestion_Frame, text="Prénom : ", font=("times new roman", 15), bg="cyan")
prenom.place(x=5, y=140, width=210)
prenom_txt = Entry(Gestion_Frame, textvariable=self.prenom, font=("times new roman", 15), bg="lightgray")
prenom_txt.place(x=150, y=140, width=300)
# mail
mail = Label(Gestion_Frame, text="Mail : ", font=("times new roman", 15), bg="cyan")
mail.place(x=5, y=170, width=230)
mail_txt = Entry(Gestion_Frame, textvariable=self.mail, font=("times new roman", 15), bg="lightgray")
mail_txt.place(x=150, y=170, width=300)
# telephone
telephone = Label(Gestion_Frame, text="Téléphone : ", font=("times new roman", 15), bg="cyan")
telephone.place(x=5, y=200, width=188)
telephone_txt = Entry(Gestion_Frame, textvariable=self.telephone, font=("times new roman", 15), bg="lightgray")
telephone_txt.place(x=150, y=200, width=300)
# date de naissance
date = Label(Gestion_Frame, text="Date : ", font=("times new roman", 15), bg="cyan")
date.place(x=500, y=50, width=235)
date_txt = Entry(Gestion_Frame, textvariable=self.date, font=("times new roman", 15), bg="lightgray")
date_txt.place(x=650, y=50, width=300)
# cotisation
cotisation = Label(Gestion_Frame, text="Cotisation : ", font=("times new roman", 15), bg="cyan")
cotisation.place(x=500, y=80, width=190)
cotisatione_txt = Entry(Gestion_Frame, textvariable=self.cotisation, font=("times new roman", 15), bg="lightgray")
cotisatione_txt.place(x=650, y=80, width=300)
# Ville
ville = Label(Gestion_Frame, text="Ville : ", font=("times new roman", 15), bg="cyan")
ville.place(x=500, y=110, width=230)
ville_txt = Entry(Gestion_Frame, textvariable=self.ville, font=("times new roman", 15), bg="lightgray")
ville_txt.place(x=650, y=110, width=300)
# code_postal
code_postal = Label(Gestion_Frame, text="code_postal : ", font=("times new roman", 15), bg="cyan")
code_postal.place(x=500, y=140, width=172)
code_postal_txt = Entry(Gestion_Frame, textvariable=self.code_postal, font=("times new roman", 15), bg="lightgray")
code_postal_txt.place(x=650, y=140, width=300)
# adresse
adresse = Label(Gestion_Frame, text="Adresse : ", font=("times new roman", 15), bg="cyan")
adresse.place(x=500, y=170, width=205)
self.adresse = Text(Gestion_Frame, font=("times new roman", 15))
self.adresse.place(x=650, y=170, width=300, height=90)
# Bouton ajouter
btn_ajouter = Button(Gestion_Frame, command=self.ajou_etudiant, text="Ajouter", font=("times new roman", 15), bd=10, relief=GROOVE, bg="green")
btn_ajouter.place(x=1360,y=10, width=120)
# Bouton modifier
btn_modifier = Button(Gestion_Frame, command=self.modifier, text="Modifier", font=("times new roman", 15), bd=10, relief=GROOVE, bg="yellow")
btn_modifier.place(x=1360,y=75, width=120)
# Bouton Supprimer
btn_supprimer = Button(Gestion_Frame, command=self.supprimer, text="Supprimer", font=("times new roman", 15), bd=10, relief=GROOVE, bg="red")
btn_supprimer.place(x=1360,y=205, width=120)
# Bouton Reinitialiser
btn_reinitialiser = Button(Gestion_Frame, command=self.reini, text="Reinitialiser", font=("times new roman", 15), bd=10, relief=GROOVE, bg="gray")
btn_reinitialiser.place(x=1360,y=140, width=120)
###########################
# Recherche
Details_Frame = Frame(self.root, bd=5, relief=GROOVE, bg="cyan")
Details_Frame.place(x=7, y=300, width=1495, height=495)
Affiche_resultat = Label(Details_Frame, text="Recherche par", font=("times new roman", 20, "bold"),bg="cyan")
Affiche_resultat.place(x=10, y=30)
rech = ttk.Combobox(Details_Frame, textvariable=self.recherch_par, font=("times new roman", 15), state="readonly")
rech["values"]=(" Id", " Nom", " Ville")
rech.place(x=190, y=35, width=160, height=35)
#rech.current(0)
rech_txt = Entry(Details_Frame, textvariable=self.recherch, font=("times new roman", 15), bd=5, relief=GROOVE)
rech_txt.place(x=370,y=35, width=200, height=35)
btn_rech =Button(Details_Frame, command=self.rechercher_info, text="Rechercher", font=("times new roman", 10), bd=5, bg="gray", relief=GROOVE)
btn_rech.place(x=580, y=35, width=120, height=35)
btn_afftou =Button(Details_Frame, command=self.afficherRechertat , text="Afficher tous", font=("times new roman", 10), bd=5, bg="gray", relief=GROOVE)
btn_afftou.place(x=710, y=35, width=120, height=35)
btn_quit =Button(Details_Frame, command=self.quitte , text="Quitter", font=("times new roman", 10,"bold"), bd=5, bg="red", relief=GROOVE)
btn_quit.place(x=840, y=35, width=120, height=35)
btn_quit =Button(Details_Frame, command=self.imprimer , text="Imprimer", font=("times new roman", 10,"bold"), bd=5, bg="green", relief=GROOVE)
btn_quit.place(x=1100, y=35, width=120, height=35)
######################
# Affichage bas
result_Frame =Frame(Details_Frame, bd=5, relief=GROOVE, bg="cyan")
result_Frame.place(x=10, y=100, width=1475, height=360)
scroll_x = Scrollbar(result_Frame, orient=HORIZONTAL)
scroll_y = Scrollbar(result_Frame, orient=VERTICAL)
self.tabl_resul = ttk.Treeview(result_Frame, columns=("Id", "Titre", "Nom", "Prenom", "Mail", "Telephone", "Date", "Cotisation", "Ville", "Code_postal", "Adresse"),
xscrollcommand=scroll_x.set,
yscrollcommand=scroll_y.set)
scroll_x.pack(side=BOTTOM, fill=X)
scroll_y.pack(side=RIGHT, fill=Y)
# nom colonne affichage
self.tabl_resul.heading("Id", text="Id") # id
self.tabl_resul.heading("Titre", text="Titre") # Titre
self.tabl_resul.heading("Nom", text="Nom") # Nom
self.tabl_resul.heading("Prenom", text="Prénom") # Prenom
self.tabl_resul.heading("Mail", text="Mail") # Mail
self.tabl_resul.heading("Telephone", text="Telephone") # Telephone
self.tabl_resul.heading("Date", text="Date") # Date
self.tabl_resul.heading("Cotisation", text="Cotisation") # cotisation
self.tabl_resul.heading("Ville", text="Ville") # ville
self.tabl_resul.heading("Code_postal", text="Code_postal") # code_postal
self.tabl_resul.heading("Adresse", text="Adresse") # Adresse
self.tabl_resul["show"]="headings"
# largeur colonne affichage
self.tabl_resul.column("Id", width=20) # id
self.tabl_resul.column("Titre", width=40) # Titre
self.tabl_resul.column("Nom", width=150) # Nom
self.tabl_resul.column("Prenom", width=150) # Prenom
self.tabl_resul.column("Mail", width=200) # Mail
self.tabl_resul.column("Telephone", width=100) # Telephone
self.tabl_resul.column("Date", width=100) # Date
self.tabl_resul.column("Cotisation", width=80) # cotisation
self.tabl_resul.column("Ville", width=150) # ville
self.tabl_resul.column("Code_postal", width=80) # code_postal
self.tabl_resul.column("Adresse", width=300) # Adresse
self.tabl_resul.pack()
self.tabl_resul.bind("<ButtonRelease-1>", self.information)
self.afficherRechertat()
def ajou_etudiant(self):
if self.id.get()=="" or self.nom.get()=="" or self.prenom.get()=="":
messagebox.showerror("Erreur", "Vous n'avez pas rempli les champs obligatoires", parent=self.root)
else:
# nom en majuscule
strg = self.nom.get()
self.nom = strg.upper()
strg = ""
# prem. lettre en majuscule
strg=self.prenom.get()
self.prenom = strg.title()
strg = ""
# chaine en minuscule
strg=self.mail.get()
self.mail = strg.lower()
strg = ""
con = sqlite3.connect("creerinscript.db")
cur = con.cursor()
cur.execute("INSERT INTO formordi VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
((self.id.get(), # Id
self.titre.get(), # Titre
self.nom.get(), # Nom
self.prenom.get(), # Prenom
self.mail.get(), # Mail
self.telephone.get(), # Telephone
self.date.get(), # Date
self.cotisation.get(), # cotisation
self.ville.get(), # ville
self.code_postal.get(), # code postale
self.adresse.get("1.0", END) ))) # Adresse
con.commit()
self.afficherRechertat()
self.reini()
con.close()
messagebox.showinfo("Succes", "Enregistrement Effectue")
def afficherRechertat(self):
con = sqlite3.connect("creerinscript.db")
cur = con.cursor()
cur.execute("SELECT * FROM formordi")
rows = cur.fetchall()
if len(rows)!=0:
self.tabl_resul.delete(* self.tabl_resul.get_children())
for row in rows:
self.tabl_resul.insert("", END, values=row)
con.commit()
con.close()
# reinitialisation
def reini(self):
self.id.set("") # id
self.titre.set("") # Titre
self.nom.set("") # Nom
self.prenom.set("") # Prenom
self.mail.set("") # mail
self.telephone.set("") # Telephone
self.date.set("") # Date
self.cotisation.set("") # cotisation
self.ville.set("") # ville
self.code_postal.set("") # code_postal
self.adresse.delete("1.0", END) # Adresse
# information
def information(self, ev):
cursors_row = self.tabl_resul.focus()
contents = self.tabl_resul.item(cursors_row)
row =contents["values"]
self.id.set(row[0]), # id
self.titre.set(row[1]), # Titre
self.nom.set(row[2]), # Nom
self.prenom.set(row[3]), # Prenom
self.mail.set(row[4]), # mail
self.telephone.set(row[5]), # Telephone
self.date.set(row[6]), # Date
self.cotisation.set(row[7]), # cotisation
self.ville.set(row[8]), # ville
self.code_postal.set(row[9]), # code_postal
self.adresse.delete("1.0", END) # Adresse
self.adresse.insert(END, row[10]), # Adresse
# Modifier
def modifier(self):
con = sqlite3.connect("creerinscript.db")
cur = con.cursor()
cur.execute("update formordi set titre=?, nom=?, prenom=?, mail=?, telephone=?, date=?, cotisation=?, ville=?, code_postal=?, adresse=? where id=?",
((self.titre.get(), # Titre
self.nom.get(), # Nom
self.prenom.get(), # Prenom
self.mail.get(), # mail
self.telephone.get(), # Telephone
self.date.get(), # Date
self.cotisation.get(), # cotisation
self.ville.get(), # ville
self.code_postal.get(), # code_postal
self.adresse.get("1.0", END), # Adresse
self.id.get()))) # id
con.commit()
messagebox.showinfo("Succes", "Modification Effectue")
self.afficherRechertat()
self.reini()
con.close()
# supprimer
def supprimer(self):
con = sqlite3.connect("creerinscript.db")
cur = con.cursor()
cur.execute("delete from formordi where id=?",(self.id.get()))
con.commit()
self.afficherRechertat()
self.reini()
con.close()
# recherche info
def rechercher_info(self):
con = sqlite3.connect("creerinscript.db")
cur = con.cursor()
cur.execute("select * from formordi where " + str((self.recherch_par.get())) + " LIKE '%" + str((self.recherch.get())) + "%'")
rows = cur.fetchall()
if len(rows) !=0:
self.tabl_resul.delete(* self.tabl_resul.get_children())
for row in rows:
self.tabl_resul.insert('', END, values=row)
con.commit()
con.close()
# messagebox.showinfo("Succes", "Rechercher Effectue")
rep = askokcancel("Succés","Voulez-vous l'imprimer")
if rep == 1:
oldstd=sys.stdout
sys.stdout=io.StringIO()
print("Id : ",(row[0])), # id
print("Titre :",(row[1])), # titre
print("Nom : ",(row[2])), # Nom
print("Prénom : ",(row[3])), # Prenom
print("Mail : ",(row[4])), # Coeurs
print("Téléphone : ",(row[5])), # Threads
print("Date : ",(row[6])), # Date
print("Cotisation : ",(row[7])), # cotisation
print("Ville : ",(row[8])), # ville
print("Code_postal : ",(row[9])), # code_postal
print("Adresse : ",(row[10])), # Adresse
print("**************************************")
# print("debut")
tmp_name="fichier_test.txt"
with open(tmp_name,"w") as tmp :
tmp.write(sys.stdout.getvalue()) # texte à imprimer dans fichier temporaire
# imprimer le fichier temporaire via la commande 'notepad /P'
with open(tmp_name,'r') as tmp :
os.system('notepad /P '+tmp_name)
os.remove(tmp_name) # supprimer le fichier temporaire
# restaurer le routage standard de stdout :
sys.stdout=oldstd # en principe, sys.__stdout__ ferait l'affaire
messagebox.showinfo("Succes", "Impression Effectue")
# fin de l'impression
# imprimer
def imprimer(self):
oldstd=sys.stdout
sys.stdout=io.StringIO()
con = sqlite3.connect("creerinscript.db")
cur = con.cursor()
cur.execute("select * from formordi ")
rows = cur.fetchall()
for row in rows:
self.tabl_resul.insert('', END, values=row)
# router tous les print dans une StringIO
print("Id : ",(row[0])), # id
print("Titre :",(row[1])), # titre
print("Nom : ",(row[2])), # Nom
print("Prénom : ",(row[3])), # Prenom
print("Mail : ",(row[4])), # Coeurs
print("Téléphone : ",(row[5])), # Threads
print("Date : ",(row[6])), # Date
print("Cotisation : ",(row[7])), # cotisation
print("Ville : ",(row[8])), # ville
print("Code_postal : ",(row[9])), # code_postal
print("Adresse : ",(row[10])), # Adresse
print("**************************************")
# print("fichier fermer")
con.commit()
con.close()
# print("Fin")
tmp_name="fichier_test.txt"
with open(tmp_name,"w") as tmp :
tmp.write(sys.stdout.getvalue()) # texte à imprimer dans fichier temporaire
# imprimer le fichier temporaire via la commande 'notepad /P'
with open(tmp_name,'r') as tmp :
os.system('notepad /P '+tmp_name)
os.remove(tmp_name) # supprimer le fichier temporaire
# restaurer le routage standard de stdout :
sys.stdout=oldstd # en principe, sys.__stdout__ ferait l'affaire
messagebox.showinfo("Succes", "Impression Effectue")
# fin de l'impression
def quitte(self):
exit()
root=Tk()
obj =Etudiant(root)
root.mainloop()
j’attend qu’il fonctionne bien, pour essayer de le couper en plusieur pages
merci d’avance pour l’aide.
cordialement
kyrob