Treeview Python

Résolu/Fermé
python - Modifié le 10 mars 2023 à 18:35
 isadama - 16 mars 2023 à 17:25

Bonjour

j'ai un soucis avec l'affichage de treeview de python avec tkinter.

En effet, le treeview affiche le contenu de ma table sauf que l'affichage vient avec des caractères ' ' et des parenthèses.

Quelqu'un pourrait m'aider à régler cela?

Je vous envoi mon code et une capture de la présentation de mon treeview.

Merci d'avance§

from tkinter import *
from tkinter import messagebox, ttk
from tkcalendar import *
import database
import os
import tempfile
import pyodbc
from tkinter import ttk

class Salarie:
    def __init__(self, root):
        self.root = root
        self.root.title("Gestion de paie")
        self.root.geometry("1350x1080+0+0")
        title = Label(self.root, text="Gestion de Paie", bd=10, relief=RAISED, font=('Algerian', 10), bg='cyan', fg='black')
        title.pack(side=TOP, fill=X)

        # Les variables
        self.matricule= StringVar()
        self.etablissement = StringVar()
        self.nom = StringVar()
        self.prenom = StringVar()
        self.genre = StringVar()
        self.mail = StringVar()
        self.nationalite = StringVar()
        self.situation = StringVar()
        self.tel1 = StringVar()
        self.tel2 = StringVar()
        self.adresse = StringVar()
        self.modePaie = StringVar()
        self.crit1 = StringVar()
        self.crit2 = StringVar()
        self.crit3 = StringVar()
        self.crit4 = StringVar()
        self.crit5 = StringVar()
        self.crit6 = StringVar()
        self.crit7 = StringVar()
        self.crit8 = StringVar()
        self.crit9 = StringVar()
        self.crit10 = StringVar()
        self.crit11 = StringVar()
        self.crit12 = StringVar()
        self.varLibre1 = StringVar()
        self.varLibre2 = StringVar()
        self.varLibre3 = StringVar()
        self.varLibre4 = StringVar()
        self.varLibre5 = StringVar()
        self.varLibre6 = StringVar()
        self.varLibre7 = StringVar()
        self.varLibre8 = StringVar()
        self.observation = StringVar()


        Frame0 = Frame(self.root, bd=2, relief=RIDGE)
        Frame0.place(x=10, y=40, width=465, height=635)
        title0 = Label(Frame0, text="Liste salarié", font=("times new roman", 10), bg="lightgray", fg="black")
        title0.pack(side=TOP, fill=X)

        detail_frame = Label(Frame0, text="Rechercher par:", font=("times new roman", 12),  fg="black")
        detail_frame.place(x=8, y=30)

        rech = ttk.Combobox(Frame0, font=("times new roman", 12), state="readonly")
        rech["values"]=("code", "nom", "prenom")
        rech.place(x=110, y=30, width=100, height=25)
        rech.current(0)

        rech_txt = Entry(Frame0, font=("times new roman", 12), fg="black")
        rech_txt.place(x=215, y=30, width=150, height=25)
        btn_recher = Button(Frame0, text="Rechercher", font=("times new roman", 10, "bold"), bg="gray").place(x= 375, y=30)
        btn_affichtout = Button(Frame0, text="Afficher tout", font=("times new roman", 10, "bold"), bg="gray").place(x=375, y=60)

        #Affichage
        result_frame = Frame(Frame0, bd=2, relief=RIDGE, bg="white")
        result_frame.place(x=5, y=85, width=450, height=540)

        scroll_x = Scrollbar(result_frame, orient=HORIZONTAL)
        scroll_y = Scrollbar(result_frame, orient=VERTICAL)

        self.tabl_result = ttk.Treeview(result_frame, columns=("code", "nom", "prenom"), xscrollcommand=scroll_x.set, yscrollcommand=scroll_y.set)

        scroll_x.pack(side=BOTTOM, fill=X)
        scroll_y.pack(side=RIGHT, fill=Y)
        self.tabl_result.heading("code", text="Code", anchor=W)
        self.tabl_result.heading("nom", text="Nom", anchor=W)
        self.tabl_result.heading("prenom", text="Prénom(s)", anchor=W)

        self.tabl_result["show"] = "headings"

        self.tabl_result.column("code", anchor=W, width=80)
        self.tabl_result.column("nom", anchor=W, width=150)
        self.tabl_result.column("prenom", anchor=W, width=200)

        self.tabl_result.pack()

        self.tabl_result.bind("<ButtonRelease-1")

        self.afficher_result()




        Frame1 = Frame(self.root, bd=2, relief=RIDGE)
        Frame1.place(x=480, y=40, width=850, height=635)
        title2 = Label(Frame1, text="Informations salarié", font=("times new roman", 10), bg="lightgray")
        title2.pack(side=TOP, fill=X)

        Framecrit= Frame(self.root, bd=2, relief=RIDGE)
        Framecrit.place(x=480, y=250, width=415, height=320)
        critsimple = Label(Framecrit, text="critères simples", font=("times new roman", 9), fg="red")
        critsimple.pack(side=TOP, fill=X)

        Framecritx = Frame(self.root, bd=2, relief=RIDGE)
        Framecritx.place(x=905, y=250, width=415, height=320)
        critmultiple = Label(Framecritx, text="critères multiples", font=("times new romans", 9), fg="red")
        critmultiple.pack(side=TOP, fill=X)

        Frameimage = Frame(self.root, bd=2, relief=RIDGE)
        Frameimage.place(x=1105, y=95, width=215, height=150)

        lbl_matricule = Label(Frame1, text="Matricule", font=("times new roman", 12)).place(x=10, y=25)
        lbl_etabl = Label(Frame1, text="Etablissement", font=("times new roman", 12)).place(x=10, y=55)
        lbl_nom = Label(Frame1, text="Nom", font=("times new roman", 12)).place(x=10, y=80)
        lbl_prenom = Label(Frame1, text="Prénom(s)", font=("times new roman", 12)).place(x=10, y=105)
        lbl_genre = Label(Frame1, text="Genre", font=("times new roman", 12)).place(x=12, y=130)
        lbl_mail = Label(Frame1, text="E_mail", font=("times new roman", 12)).place(x=12, y=155)
        lbl_national = Label(Frame1, text="Nationnalité", font=("times new roman", 12)).place(x=10, y=180)
        lbl_observation = Label(Frame1, text="Observations", font=("times new roman", 12)).place(x=10, y=530)
        lbl_situation = Label(Frame1, text="Situation familiale", font=("times new roman", 12)).place(x=310, y=55)
        lbl_naissance = Label(Frame1, text="Date Naissance", font=("times new roman", 12)).place(x=310, y=80)
        lbl_telephone1 = Label(Frame1, text="Téléphone 1", font=("times new roman", 12)).place(x=310, y=105)
        lbl_telephone2 = Label(Frame1, text="Téléphone 2", font=("times new roman", 12)).place(x=310, y=130)
        lbl_adress = Label(Frame1, text="Adresse", font=("times new roman", 12)).place(x=310, y=155)
        lbl_mode_paie = Label(Frame1, text="Mode paiement", font=("times new roman", 12)).place(x=310, y=180)

        lbl_varlibre1 = Label(Frame1, text="VarLibre1", font=("times new roman", 12)).place(x=355, y=530)
        lbl_varlibre2 = Label(Frame1, text="VarLibre2", font=("times new roman", 12)).place(x=355, y=555)
        lbl_varlibre3 = Label(Frame1, text="VarLibre3", font=("times new roman", 12)).place(x=355, y=580)
        lbl_varlibre4 = Label(Frame1, text="VarLibre4", font=("times new roman", 12)).place(x=355, y=600)
        lbl_varlibre5 = Label(Frame1, text="VarLibre5", font=("times new roman", 12)).place(x=600, y=530)
        lbl_varlibre6 = Label(Frame1, text="VarLibre6", font=("times new roman", 12)).place(x=600, y=555)
        lbl_varlibre7 = Label(Frame1, text="VarLibre7", font=("times new roman", 12)).place(x=600, y=580)
        lbl_varlibre8 = Label(Frame1, text="VarLibre8", font=("times new roman", 12)).place(x=600, y=600)
        lbl_photo = Label(Frame1, text="Photo d'identité", font=("times new roman", 12)).place(x=625, y=25)

        lbl_crit1 = Label(Framecrit, text="Critère 1", font=("times new roman", 12)).place(x=10, y=17)
        lbl_crit2 = Label(Framecrit, text="Critère 2", font=("times new roman", 12)).place(x=10, y=42)
        lbl_crit3 = Label(Framecrit, text="Critère 3", font=("times new roman", 12)).place(x=10, y=67)
        lbl_crit4 = Label(Framecrit, text="Critère 4", font=("times new roman", 12)).place(x=10, y=92)
        lbl_crit5 = Label(Framecrit, text="Critère 5", font=("times new roman", 12)).place(x=10, y=117)
        lbl_crit6 = Label(Framecrit, text="Critère 6", font=("times new roman", 12)).place(x=10, y=142)
        lbl_crit7 = Label(Framecrit, text="Critère 7", font=("times new roman", 12)).place(x=10, y=166)
        lbl_crit8 = Label(Framecrit, text="Critère 8", font=("times new roman", 12)).place(x=10, y=192)
        lbl_crit9 = Label(Framecrit, text="Critère 9", font=("times new roman", 12)).place(x=10, y=217)
        lbl_crit10 = Label(Framecrit, text="Critère 10", font=("times new roman", 12)).place(x=10, y=242)
        lbl_crit11 = Label(Framecrit, text="Critère 11", font=("times new roman", 12)).place(x=10, y=267)
        lbl_crit12 = Label(Framecrit, text="Critère 12", font=("times new roman", 12)).place(x=10, y=292)

        self.matricule = Entry(Frame1, textvariable=self.matricule, font=('times new roman', 10), bg="lightyellow")
        self.matricule.place(x=115, y=25, width=100)

        self.etablissement = ttk.Combobox(Frame1, textvariable=self.etablissement, font=('times new roman', 10), state="readonly")
        self.etablissement["values"] = (1,2,3)
        self.etablissement.place(x=115, y=55, width=175)
        self.etablissement.current(0)

        self.nom = Entry(Frame1, textvariable=self.nom, font=('times new roman', 10), bg="lightyellow")
        self.nom.place(x=115, y=80, width=175)

        self.prenom = Entry(Frame1, textvariable=self.prenom, font=('times new roman', 10), bg="lightyellow")
        self.prenom.place(x=115, y=105, width=175)

        self.genre = ttk.Combobox(Frame1, textvariable=self.genre, font=('time new roman', 10), state="readonly")
        self.genre["values"] = (1,2)
        self.genre.place(x=115, y=130, width=175)
        self.genre.current(0)

        self.mail = Entry(Frame1, textvariable=self.mail,  font=('E_mail', 10), bg="lightyellow")
        self.mail.place(x=115, y=155, width=175)

        self.nationalite = ttk.Combobox(Frame1, textvariable=self.nationalite, font=('time new roman', 10), state="readonly")
        self.nationalite["values"] = (1,2,3,4)
        self.nationalite.place(x=115, y=180, width=175)
        self.nationalite.current(0)

        self.observation = Text(Frame1, font=("time new roman", 10), bg="lightyellow")
        self.observation.place(x=115, y=540, width=230, height=90)

        self.situation = ttk.Combobox(Frame1, textvariable=self.situation, font=('time new roman', 10), state="readonly")
        self.situation["values"] = (1,2,3,4)
        self.situation.place(x=440, y=55, width=175)
        self.situation.current(0)

        self.ecri_date = DateEntry(Frame1,  font=("time new roman", 10), bg="lightyellow", state="readonly", locale='fr_FR', date_pattern = 'dd/mm/yyyy')
        self.ecri_date.place(x=440, y=80, width=175)


        self.tel1 = Entry(Frame1,textvariable=self.tel1,  font=("time new roman", 10), bg="lightyellow")
        self.tel1.place(x=440, y=105, width=175)

        self.tel2 = Entry(Frame1, textvariable=self.tel2,  font=("time new roman", 10), bg="lightyellow")
        self.tel2.place(x=440, y=130, width=175)

        self.adresse = Entry(Frame1, textvariable=self.adresse, font=("time new roman", 10), bg="lightyellow")
        self.adresse.place(x=440, y=155, width=175)

        self.modePaie = ttk.Combobox(Frame1, textvariable=self.modePaie, font=('time new roman', 10), state="readonly")
        self.modePaie["values"] = (1,2,3,4,5)
        self.modePaie.place(x=440, y=180, width=175)
        self.modePaie.current(0)

        self.crit1 = ttk.Combobox(Framecrit, textvariable=self.crit1, font=('time new roman', 10), state="readonly")
        self.crit1["values"] = (1,2,3,4,5)
        self.crit1.place(x=170, y=20, width=240)
        self.crit1.current(0)

        self.crit2 = ttk.Combobox(Framecrit, textvariable=self.crit2, font=('time new roman', 10), state="readonly")
        self.crit2["values"] = (1,2,3,4,5)
        self.crit2.place(x=170, y=45, width=240)
        self.crit2.current(0)

        self.crit3 = ttk.Combobox(Framecrit, textvariable=self.crit3,  font=('time new roman', 10), state="readonly")
        self.crit3["values"] = (1,2,3,4,5)
        self.crit3.place(x=170, y=70, width=240)
        self.crit3.current(0)

        self.crit4 = ttk.Combobox(Framecrit, textvariable=self.crit4,  font=('time new roman', 10), state="readonly")
        self.crit4["values"] = (1,2,3,4,5)
        self.crit4.place(x=170, y=95, width=240)
        self.crit4.current(0)

        self.crit5 = ttk.Combobox(Framecrit, textvariable=self.crit5, font=('time new roman', 10), state="readonly")
        self.crit5["values"] = (1,2,3,4,5)
        self.crit5.place(x=170, y=120, width=240)
        self.crit5.current(0)

        self.crit6 = ttk.Combobox(Framecrit, textvariable=self.crit6, font=('time new roman', 10), state="readonly")
        self.crit6["values"] = (1,2,3,4,5)
        self.crit6.place(x=170, y=145, width=240)
        self.crit6.current(0)

        self.crit7 = ttk.Combobox(Framecrit, textvariable=self.crit7, font=('time new roman', 10), state="readonly")
        self.crit7["values"] = (1,2,3,4,5)
        self.crit7.place(x=170, y=170, width=240)
        self.crit7.current(0)

        self.crit8 = ttk.Combobox(Framecrit, textvariable=self.crit8, font=('time new roman', 10), state="readonly")
        self.crit8["values"] = (1,2,3,4,5)
        self.crit8.place(x=170, y=195, width=240)
        self.crit8.current(0)

        self.crit9 = ttk.Combobox(Framecrit, textvariable=self.crit9, font=('time new roman', 10), state="readonly")
        self.crit9["values"] = (1,2,3,4,5)
        self.crit9.place(x=170, y=220, width=240)
        self.crit9.current(0)

        self.crit10 = ttk.Combobox(Framecrit, textvariable=self.crit10, font=('time new roman', 10), state="readonly")
        self.crit10["values"] = (1,2,3,4,5)
        self.crit10.place(x=170, y=245, width=240)
        self.crit10.current(0)

        self.crit11 = ttk.Combobox(Framecrit, textvariable=self.crit11, font=('time new roman', 10), state="readonly")
        self.crit11["values"] = (1,2,3,4,5)
        self.crit11.place(x=170, y=270, width=240)
        self.crit11.current(0)

        self.crit12 = ttk.Combobox(Framecrit, textvariable=self.crit12, font=('time new roman', 10), state="readonly")
        self.crit12["values"] = (1,2,3,4,5)
        self.crit12.place(x=170, y=295, width=240)
        self.crit12.current(0)


        self.varLibre1 = Entry(Frame1, textvariable=self.varLibre1, font=('times new roman', 10), bg="lightyellow")
        self.varLibre1.place(x=445, y=535, width=140)
        self.varLibre2 = Entry(Frame1, textvariable=self.varLibre2, font=('times new roman', 10), bg="lightyellow")
        self.varLibre2.place(x=445, y=562, width=140)
        self.varLibre3 = Entry(Frame1, textvariable=self.varLibre3, font=('times new roman', 10), bg="lightyellow")
        self.varLibre3.place(x=445, y=585, width=140)
        self.varLibre4 = Entry(Frame1, textvariable=self.varLibre4, font=('times new roman', 10), bg="lightyellow")
        self.varLibre4.place(x=445, y=608, width=140)
        self.varLibre5 = Entry(Frame1, textvariable=self.varLibre5, font=('times new roman', 10), bg="lightyellow")
        self.varLibre5.place(x=700, y=535, width=140)
        self.varLibre6 = Entry(Frame1, textvariable=self.varLibre6, font=('times new roman', 10), bg="lightyellow")
        self.varLibre6.place(x=700, y=562, width=140)
        self.varLibre7 = Entry(Frame1, textvariable=self.varLibre7, font=('times new roman', 10), bg="lightyellow")
        self.varLibre7.place(x=700, y=585, width=140)
        self.varLibre8 = Entry(Frame1, textvariable=self.varLibre8, font=('times new roman', 10), bg="lightyellow")
        self.varLibre8.place(x=700, y=608, width=140)

        btn_ajout = Button( text="Ajouter", command=self.ajout_salarie, font=("times new roman", 10, "bold"),relief=GROOVE, bg="green").place(x=11, y=675)
        btn_modif = Button(text="Modifier", font=("times new roman", 10, "bold"),relief=GROOVE, bg="yellow").place(x=75, y=675)
        btn_suppr = Button(text="Supprimer", font=("times new roman", 10, "bold"),relief=GROOVE, bg="red").place(x=145, y=675)
        btn_reini = Button(text="Réinitialiser", font=("times new roman", 10, "bold"),relief=GROOVE, bg="blue").place(x=225, y=675)

    def reini(self):
        self.matricule.delete(0, END)
        self.etablissement.delete(0, END)
        self.nom.delete(0, END)
        self.prenom.delete(0, END)
        self.genre.delete(0, END)
        self.mail.delete(0, END)
        self.nationalite.delete(0, END)
        self.situation.delete(0, END)
        self.ecri_date.set_date("03/03/2022")
        self.tel1.delete(0, END)
        self.tel2.delete(0, END)
        self.adresse.delete(0, END)
        self.modePaie.delete(0, END)
        self.crit1.delete(0, END)
        self.crit2.delete(0, END)
        self.crit3.delete(0, END)
        self.crit4.delete(0, END)
        self.crit5.delete(0, END)
        self.crit6.delete(0, END)
        self.crit7.delete(0, END)
        self.crit8.delete(0, END)
        self.crit9.delete(0, END)
        self.crit10.delete(0, END)
        self.crit11.delete(0, END)
        self.crit12.delete(0, END)
        self.varLibre1.delete(0, END)
        self.varLibre2.delete(0, END)
        self.varLibre3.delete(0, END)
        self.varLibre4.delete(0, END)
        self.varLibre5.delete(0, END)
        self.varLibre6.delete(0, END)
        self.varLibre7.delete(0, END)
        self.varLibre8.delete(0, END)
        self.observation.delete("1.0", "end")



    def ajout_salarie(self):
        if self.matricule.get() == "" or self.nom.get() == "" or self.prenom.get() == "":
            messagebox.showerror("Attention:", "Vous n'avez pas rempli les champs obligatoires", parent=self.root)
        else:

            try:
                cursor = database.conn.cursor()
                cursor.execute("select* from salarie where matricule=?", self.ecri_date.get())
                row = cursor.fetchone()

                if row != None:
                    messagebox.showerror("Erreur:", "Cette données existe déjà", parent=self.root)

                else:
                    cursor.execute(
                        "INSERT INTO salarie(Matricule,Etabl,Nom,Prenoms,Sexe,PaysNationalite,Email,SituationFamiliale,DateNaissance,"
                        "Telephone1,Telephone2, Adresse,ModePaiement,Crit1,Crit2,Crit3,Crit4,Crit5,Crit6,Crit7,Crit8"
                        ",Crit9,Crit10,Crit11,Crit12,VarLibre1,VarLibre2,VarLibre3,VarLibre4,VarLibre5,VarLibre6,VarLibre7,VarLibre8,ObSalarie) "
                        "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                        (self.matricule.get(), self.etablissement.get(), self.nom.get(), self.prenom.get(), self.genre.get(),
                         self.nationalite.get(), self.mail.get(), self.situation.get(), self.ecri_date.get(), self.tel1.get(),
                         self.tel2.get(), self.adresse.get(), self.modePaie.get(), self.crit1.get(), self.crit2.get(),
                         self.crit3.get(), self.crit4.get(), self.crit5.get(), self.crit6.get(), self.crit7.get(),
                         self.crit8.get(), self.crit9.get(), self.crit10.get(), self.crit11.get(), self.crit12.get(),
                         self.varLibre1.get(), self.varLibre2.get(), self.varLibre3.get(), self.varLibre4.get(),
                         self.varLibre5.get(), self.varLibre6.get(), self.varLibre7.get(), self.varLibre8.get(), self.observation.get("1.0", END)))
                    database.conn.commit()
                    self.afficher_result()
                    #database.conn.close()
                messagebox.showinfo("Salarié enregistré avec succès")
                self.reini()

            except Exception as es:
                messagebox.showerror("Erreur:", f" {str(es)}", parent=self.root)

    def afficher_result(self):
        cursor = database.conn.cursor()
        cursor.execute("select matricule, nom, prenoms from salarie")
        rows = cursor.fetchall()
        if len(rows) != 0:
            self.tabl_result.delete(* self.tabl_result.get_children())
            for row in rows:
                self.tabl_result.insert("", END, values=row)
            database.conn.commit()
        #database.conn.close()





root = Tk()
obj = Salarie(root)
root.mainloop()

2 réponses

yg_be Messages postés 22707 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 19 avril 2024 1 471
10 mars 2023 à 21:26

bonjour,

peux-tu partager ton module database?

0

Bonjour

je vous envoi mon module database ici.

Mais ma requête de sélection

import pyodbc
driver = "{ODBC Driver 13 for SQL Server}"
server ="DESKTOP-G76772H\SQLEXPRESS"
database = "TRAITEMENT"
username = "sa"
password = "Admin321"
conn = pyodbc.connect("DRIVER=" + driver
                                + ";SERVER=" + server
                                + ";DATABASE=" + database
                                + ";UID=" + username
                                + ";PWD=" + password)

cursor = conn.cursor()

for row in cursor.execute("Select matricule, nom, prenoms from salarie"):
    print(row.matricule, row.nom, row.prenoms)

fonctionne bien dans sql.

Seulement c'est la présentation du tableau de liste qui se présente pas bien.

Il des caractères supplémentaires qui viennent s'ajouter.

Cordialement,

0
yg_be Messages postés 22707 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 19 avril 2024 1 471 > python
12 mars 2023 à 23:31

Ce qui confirme l'analyse de hystouar en #2: tes données ne sont pas enregistrées comme tu le souhaites dans ta base de données.

0
isadama > yg_be Messages postés 22707 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 19 avril 2024
16 mars 2023 à 09:58

Bonjour

quand je fait un simple print le résultat s'affiche correctement.

Mais dans le tableau le résultat n'est pas correct.

je vous envoi une image de print row

w dans ce message.

Merci d'avance.

0

Bonjour

je constate par exemple un affichage avec une autre base de données.

je ne comprend pas pourquoi mes valeurs du treeview sont entre parenthèse et griffe et séparées par des virgules.

isadama

0
yg_be Messages postés 22707 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 19 avril 2024 1 471 > isadama
16 mars 2023 à 10:30

Partage la source du programme qui fait ce print().

0

Bonsoir, je pense que tes valeurs sont mals enregistrées, et que ce qui est affiché est ce qui est dans ta table salarie. As-tu vérifié ? Un simple print de row dans ta méthode afficher_result.

0

Bonjour

quand je fait un simple print le résultat s'affiche correctement.

Mais dans le tableau le résultat n'est pas correct.

je vous envoi une image de print row

0

Bonjour

je constate par exemple un affichage avec une autre base de données.

je ne comprend pas pourquoi mes valeurs du treeview sont entre parenthèse et griffe et séparées par des virgules.

0