# coding:utf-8 #version 3.x python # --------------------------------------------------------- # Huiles Essentiels # --------------------------------------------------------- from tkinter import * from tkinter.ttk import * from tkinter.ttk import Button from tkinter import Toplevel from tkinter import font from tkinter.scrolledtext import ScrolledText from tkinter import messagebox # Librairie des fenêtres d'alerte import HE_DB_BackEnd # import HE_DB_BackEnd.py def HE_Contenu(): # Variables HENom = StringVar() HEProp = StringVar() HEDos = StringVar() m2 = Toplevel() m2.title('Huiles Essentiels') m2.geometry("820x559+30+30") m2.resizable(width=False, height=False) # Fenêtre verrouillée m2.attributes("-toolwindow", 1) # Supprime les boutons Réduire/Agrndir # ============================== Fonctions ======================== # Vide widget Entry def ClearEntry(): txt_HENom.delete(0, END) txt_HEProp.delete('1.0', END) txt_HEDos.delete(0, END) # Affiche dans widget Entry, via souris, la sélection des Datas [Colonnes Treeview] def selectItem(a): ClearEntry() global Liste Liste = [] curItem = HElist.focus() Liste = HElist.item(curItem)["values"] # affiche Liste print("selectItem - Liste", Liste) # Mode Console print("Contenu de la sélection - Liste ", Liste) print("Colonne ID -->", Liste[0]) # affiche data colonne ID print("Colonne HE -->", Liste[1]) # affiche data colonne HE print("Colonne Description -->", Liste[2]) # affiche data colonne Description print("Colonne Dosage -->", Liste[3]) # affiche data colonne Dosage print('\n') # Mode Widget Entry txt_HENom.insert(END, Liste[1]) txt_HEProp.insert("0.0", Liste[2]) txt_HEDos.insert(END, Liste[3]) # Suppression enregistrement dans Table def DeleteData(): print("Index", Liste[0], Liste[1]) if (Liste[1] != ""): print("Index", Liste[0], Liste[1]) HE_DB_BackEnd.deleteRec(Liste[0]) Refresh_TreeView() # Rafraichissement TreeView def Refresh_TreeView(): # Recensement nombre d'enregistrements print(" - Nombre d'enregistrement _ lecture Treeview", len(HElist.get_children())) for i in range(len(HElist.get_children())): print("Index", i) # Supprime tous enregistrement du TreeView for i in HElist.get_children(): HElist.delete(i) ClearEntry() DisplayData() # Affiche la Table dans TreeView def DisplayData(): # Insertion Table BD dans TreeView a = 0 for i in HE_DB_BackEnd.loadRecord(): HElist.insert('', 'end', text=i[0], values=(i[0], i[1], i[2], i[3])) print("Index", i[0]) a = a + 1 print(" - Nombre d'enregistrement _ lecture Table", a) print(" - Nombre d'enregistrement _ lecture Treeview", len(HElist.get_children())) # Ajoute enregistrements dans Table def addData(): HEProp = txt_HEProp.get("1.0", "end-1c") # Variable de la saisie du Widget Text print("addData",type(HEProp), HEProp) # if txt_HENom.get() != "": HE_DB_BackEnd.addRec(txt_HENom.get(), HEProp, txt_HEDos.get()) Refresh_TreeView() # # Modification d'un enregistrement dans Table def update(): print("update -- Colonne ID -->", Liste[0]) # affiche data colonne ID if (txt_HENom.get()) != "": if (Liste[1] != ""): print("Table [ID - HE_Nom - HE_Prop - HE_Dos]", Liste[0], Liste[1], Liste[2], Liste[3]) HE_DB_BackEnd.deleteRec(Liste[0]) addData() Refresh_TreeView() # # Cherche enregistrement dans Table def searchDataBase(): print("txt_HENom.get()", txt_HENom.get()) HEProp = txt_HEProp.get("1.0", "end-1c") # Variable de la saisie du Widget Text print("txt_HEProp.get", HEProp) print("txt_HEDos.get()", txt_HEDos.get()) # Empty_TreeView() # for row in HE_DB_BackEnd.searchDataTable(var_HENom, var_HEDos): for row in HE_DB_BackEnd.searchDataTable(HEProp, txt_HEDos.get(),txt_HENom.get() ): # # HElist.insert('', 'end', text=i[0], values=(i[0], i[1], i[2], i[3])) print("Index", row[0], row[1], row[2], row[3]) # # print(rows) # Affiche tous les enregistrements # print(rows[0]) # Affiche l'enregistrement ligne 0 # for item in rows: # Affiche tous les enregistrements en colonne HE_Nom # print(item[1]) # Vide TreeView def Empty_TreeView(): # Recensement nombre d'enregistrements print(" - Nombre d'enregistrement _ lecture Treeview", len(HElist.get_children())) for i in range(len(HElist.get_children())): print("Index", i) # Supprime tous enregistrement du TreeView for i in HElist.get_children(): HElist.delete(i) def iEXIT(): iEXIT = messagebox.askokcancel("Attention", "Voulez-vous vraiment quitter ? ",parent=m2) if iEXIT == 1: m2.destroy() # # Widget Entry # def onClick_Dtr(event, obj): # # Supprime la valeur de la variable # print("obj", obj) # obj.delete(0, END) # # # Widget Text # def onClick_Dtr_txt(event, obj): # # Supprime la valeur de la variable # print("obj", obj) # txt_HEProp.delete('1.0', END) # ============================== Cadres ============================== MainFrame = LabelFrame(m2) MainFrame.place(x=5, y=5, width=810, height=550) FrameLeft = LabelFrame(MainFrame, text="Description") FrameLeft.place(x=5, y=5, width=300, height=170) FrameRight = LabelFrame(MainFrame, text="Liste") FrameRight.place(x=310, y=5, width=373, height=205) FrameBt = LabelFrame(MainFrame, text="boutons",relief=RIDGE) FrameBt.place(x=5, y=450, width=650, height=60) # ============================== labels & Entry Widget ============================== lblHENom = Label(FrameLeft, font=('verdana', 8, 'bold'), text="Nom") lblHENom.place(x=2, y=2) txt_HENom = Entry(FrameLeft, font=('verdana', 8, ''), textvariable=HENom, width = 25) txt_HENom.place(x=80, y=5) # txt_HENom.bind("", lambda event, obj=txt_HENom: onClick_Dtr(event, obj)) lblHEProp = Label(FrameLeft, font=('verdana', 8, 'bold'), text="Propriétès") lblHEProp.place(x=2, y=25) txt_HEProp = Text(FrameLeft, font=('verdana', 8, '')) txt_HEProp = ScrolledText(FrameLeft) txt_HEProp.place(x=80, y=25, width=210, height=100) # txt_HEProp.bind("", lambda event, obj=txt_HEProp: onClick_Dtr_txt(event, obj)) lblHEDos = Label(FrameLeft, font=('verdana', 8, 'bold'), text="Dosage") lblHEDos.place(x=2, y=128) txt_HEDos = Entry(FrameLeft, font=('verdana', 8, ''), textvariable=HEDos, width = 5) txt_HEDos.place(x=80, y=128) lblHEDos = Label(FrameLeft, font=('verdana', 8, 'bold'), text="gouttes") lblHEDos.place(x=114, y=128) # txt_HEDos.bind("", lambda event, obj=txt_HEDos: onClick_Dtr(event, obj)) # ============================== Treeview & Scrollbar Vertical [Y] ======================== scrollbar_y = Scrollbar(FrameRight) # Ascenseur Vertical scrollbar_y.place(x=351, y=2, height=181) # Treeview génère pour chauqe ligne un dictionnaire composé de : # {'text': '', 'image': '', 'values': [], 'open': 0, 'tags': ''} # Exemple {'text': 79, 'image': '', 'values': [79, 'Lavandin', 'Cicatrisantes ', ''], 'open': 0, 'tags': ''} # Les datas des colonnes de chaque ligne sont dans une liste 'values' HElist = Treeview(FrameRight, selectmode="browse", columns=(1, 2, 3, 4), show="headings", yscrollcommand=scrollbar_y.set) # En-tête HElist.heading('#1', text="ID") HElist.heading('#2', text="HE") HElist.heading('#3', text="Description") HElist.heading('#4', text="Dosage") HElist.column('#1', width=0, minwidth=0, stretch=False) HElist.column('#2', width=150, minwidth=150, stretch=OFF) HElist.column('#3', width=150, minwidth=150, stretch=OFF) HElist.column('#4', width=50, minwidth=50, stretch=OFF) HElist.place(x=2, y=2, width=350, height=180) # Cache colonne(s) exclusionlist = ['1'] # Colonne [ID] displaycolumns = ['2', '3', '4'] for col in HElist["columns"]: if not "%s" % col in exclusionlist: displaycolumns.append(col) HElist["displaycolumns"] = displaycolumns scrollbar_y.config(command=HElist.yview) # Ascenseur Vertical HE_DB_BackEnd.loadRecord() DisplayData() # Affiche dans widget Entry, via souris, la sélection des Datas [Colonnes Treeview] HElist.bind('', selectItem) # Le bouton de la souris a été relâché # ============================== Button Widget ======================== # Configuration Police de tout les boutons style = Style() style.configure('TButton', font=('verdana', 8, ''),foreground='black') # btnAddData = Button(FrameBt, text="Ajouter", style='TButton', command=addData) btnAddData.place(x=2, y=2) # btnUpdateData = Button(FrameBt, text="Modifier", style='TButton', command=update) btnUpdateData.place(x=90, y=2) # btnDeleteData = Button(FrameBt, text="Supprimer", style='TButton', command=DeleteData) btnDeleteData.place(x=210, y=2) # btnClearEntry = Button(FrameBt, text="Effacer description", style='TButton', command=ClearEntry) btnClearEntry.place(x=300, y=2) # btnExitData = Button(FrameBt, text="Sortir", style='TButton', command=iEXIT) btnExitData.place(x=420, y=2) # btnSearchData = Button(FrameBt, text="Chercher", style='TButton', command=searchDataBase) btnSearchData.place(x=510, y=2)