# coding:utf-8 #version 3.x python from tkinter import * from tkinter.ttk import * import HV_DB_BackEnd # Liaison HV_DB_BackEnd.py class Main: def __init__(self, root): self.root = root self.root.title("--------------") self.root.geometry("1420x660+0+0") self.root.config(bg="cadet blue") if __name__ =='__main__': root = Tk() application = Main(root) nb = IntVar() m = IntVar() nb = 0 # Compte de nombre de clic (sélection) fait sur TreeView m = 0 # ============================== Frames ======================== MainFrame = LabelFrame(root, text="[TEST]", relief=FLAT) MainFrame.place(x=5, y=5, width=1300, height=600) FrameCalc_1 = Frame(name='labels', relief=RIDGE) FrameCalc_1.place(x=5, y=220, width=800, height=300) # ============================== Fonctions ======================== # [Treeview - Heading] - Empêche le redimensionnement des entêtes def handle_click(event): if HElist.identify_region(event.x, event.y) == "separator": if HElist.identify_column(event.x) == '#1': return "break" if HElist.identify_column(event.x) == '#2': return "break" if HElist.identify_column(event.x) == '#3': return "break" if HElist.identify_column(event.x) == '#4': return "break" if HElist.identify_column(event.x) == '#5': return "break" if HElist.identify_column(event.x) == '#6': return "break" # Affiche dynamiquement Label selon la sélection faite dans TreeView def selectItem(a): global nb global m print(nb) print("a", a) # print("type(a)", type(a)) # global L_SUPP global Liste_bis global Liste Liste = [] Liste_bis = [] L_SUPP = [] curItem = HElist.focus() Liste = HElist.item(curItem)["values"] print("selectItem - Liste", Liste) # Affiche Liste # Mode Console - Affiche les colonnes print("Contenu de la sélection - Liste ", Liste) print("Colonne ID -->", Liste[0]) print("Colonne Nom Alcalis -->", Liste[1]) print("Colonne KOH -->", Liste[3]) print("Colonne NaOH -->", Liste[5]) print('\n') # Génère automatiiquement if Liste[1] != "": i = 1 print("Liste[1]", Liste[1]) Label(FrameCalc_1, name='%d' % i + str(m), text=Liste[1]).place(x=5, y= 4 + m) print("ID-Label_1", id('%d' % i + str(m))) # Identifie le nom unique du Label Widget (tkinter est obligé de créer un nom unique pour fabriquer la variable TCL/Tk qui correspondra au widget. l'unicité est assurée par la fabrication du nom à partir de l'id(...) qui est unique.) Liste_bis.append(Liste[1]) k = id('%d' % i + str(m)) L_SUPP.append(k) if Liste[3] != "": i = i + 1 print("Liste[3]", Liste[3]) Label(FrameCalc_1, name='%d' % i + str(m), text=Liste[3]).place(x=165, y= 4 + m) print("ID-Label_3", id('%d' % i + str(m))) Liste_bis.append(Liste[3]) k = id('%d' % i + str(m)) L_SUPP.append(k) if Liste[5] != "": i = i +1 print("Liste[5]", Liste[5]) Label(FrameCalc_1, name='%d' % i + str(m), text=Liste[5]).place(x=222, y= 4 + m) print("ID-Label_5", id('%d' % i + str(m))) Liste_bis.append(Liste[5]) k = id('%d' % i + str(m)) L_SUPP.append(k) if Liste[0] != "": Label(FrameCalc_1, name='%d' % i + str(m * 2), text="Supprimer").place(x=400, y= 4 +m) print("ID-Label_Supprimer", id('%d' % i + str(m * 2))) k = id('%d' % i + str(m * 2)) L_SUPP.append(k) print(L_SUPP) print(Liste_bis) nb = nb + 1 m = m + 18 for j in enumerate(L_SUPP): print(j) # [TreeView widgets] - Affiche Table def DisplayData(): # Insertion Table BD dans TreeView a = 0 for i in HV_DB_BackEnd.loadRecord(): HElist.insert('', 'end', text=i[0], values=(i[0], i[1], i[2], i[3], i[4], i[5])) print("Index", i[0]) a = a + 1 print(" - Nombre d'enregistrement _ lecture Table", a) print(" - Nombre d'enregistrement _ lecture Treeview", len(HElist.get_children())) # ============= Style Police : Treeview_Button_Label ============== # Le Style() est une préconfiguration d'un type de widget. La nomenclature du Nom du Style : 'T + Nom_Widget' # foreground : couleur TexT # background : couleur de fond # rowheight : hauteur de ligne treeview style = Style() # Treeview style.configure('.', foreground="black") # Configuration TxT de la Frame contenant Treeview style.configure('TTreeview', rowheight=15, font=('verdana', 8, ''), foreground='white', background='dim gray') style.configure('TTreeview.Heading', font=('verdana', 8, ''), foreground="black", cursor='none') style.layout('TTreeview', []) # style.map("TTreeview") # ============================== Treeview & Scrollbar Vertical/Horizontal [XY] ======================== scrollbar_y = Scrollbar(MainFrame, orient='vertical') # Ascenseur Vertical scrollbar_y.place(x=1236, y=24, height=169) scrollbar_x = Scrollbar(MainFrame, orient='horizontal') # Ascenseur Horizontal scrollbar_x.place(x=1, y=177, width=1236) # Treeview génère pour chaque ligne un dictionnaire composé de : # {'text': '', 'imHVe': '', '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(MainFrame, style='TTreeview', selectmode="browse", columns=(1,2,3,4,5,6), show="headings", yscrollcommand=scrollbar_y.set, xscrollcommand=scrollbar_x.set) # En-tête HElist.heading('#1', text="ID") HElist.heading('#2', text="Nom") HElist.heading('#3', text="INSI") HElist.heading('#4', text="IODE") HElist.heading('#5', text="KOH") HElist.heading('#6', text="NaOH") HElist.column('#1', width=0, minwidth=0, stretch=False) HElist.column('#2', width=160, minwidth=160, stretch=OFF) HElist.column('#3', width=50, minwidth=50, anchor=CENTER, stretch=OFF) HElist.column('#4', width=57, minwidth=57, anchor=CENTER, stretch=OFF) HElist.column('#5', width=120, minwidth=120, stretch=OFF) HElist.column('#6', width=200, minwidth=200, stretch=OFF) HElist.place(x=2, y=2, width=1236, height=175) # Cache colonne(s) exclusionlist = ['1'] # Colonne [ID] exclue d'affichage displaycolumns = ['2', '3', '4', '5', '6'] # Colonne [xxx] affichées for col in HElist["columns"]: if not "%s" % col in exclusionlist: pass HElist["displaycolumns"] = displaycolumns scrollbar_y.config(command=HElist.yview) # Ascenseur Vertical scrollbar_x.config(command=HElist.xview) # Ascenseur Horizontal HV_DB_BackEnd.loadRecord() DisplayData() # Affiche Entry widget, via souris, la sélection des Datas [Colonnes Treeview] HElist.bind('', selectItem) # Le bouton de la souris a été relâché # Désactive le redimensionnement des Entêtes Treeview HElist.bind('', handle_click) # Gauche HElist.bind('', handle_click) # Molette HElist.bind('', handle_click) # Droite root.mainloop()