Apr-19-2020, 09:21 AM
hi everyone,
on this code which automatically generates a frame with encapsulated labels, I would like to generate a maximum of 4 frames. Until then, it works. The problem is that the variable i (number of frames) is not updated when one of the frames is deleted. The variable r is the one I have to retrieve to update the value i. I tried with return and callback without success.
on this code which automatically generates a frame with encapsulated labels, I would like to generate a maximum of 4 frames. Until then, it works. The problem is that the variable i (number of frames) is not updated when one of the frames is deleted. The variable r is the one I have to retrieve to update the value i. I tried with return and callback without success.
try: from Tkinter import * except: from tkinter import * from tkinter import messagebox # Librairie des fenêtres d'alerte def removeLabel(var): global frames global i z = -1 for frame in frames: z = z + 1 # Check the text variable of the label of this frame if frame.winfo_children()[4].var == var: # 4 est le nombre de labels à générer dans une frame # Supprime frame frame.destroy() # Mise à jour du nombre de frame frames = frames[:z] + frames[z+1:] z = -1 # Mise à jour du nombre de frame r = 0 for frame in frames: frame.winfo_children()[4].var.set(r) r = r + 1 i = i - 1 print("i", i) def addNewLabel(): global frames, i global table var = IntVar() frame = Frame(root) i = i + 1 if i <= 5: frame.grid(row=i, column=4) var.set(len(frames)) l = Label(frame, text="test") l.grid(row=0, column=0) l = Label(frame, textvariable=var) l.grid(row=0, column=1) l = Label(frame, textvariable=var) l.grid(row=0, column=2) l = Label(frame, textvariable=var) l.grid(row=0, column=3) l = Label(frame, textvariable=var) l.grid(row=0, column=4) l.var = var b = Button(frame, text="Remove", command=lambda: removeLabel(var)) b.grid(row=0, column=5) frames.append(frame) # liste des frames générée print("var.get() ", var.get(), " i ",i) # var.trace("w", lambda name, index, mode, i=i: removeLabel(i)) # print("eeeee", i) if i > 5: # Limite du nombre de 10 frames à générer messagebox.showinfo("Info","Seulement 10 huiles peuvent être sélectionées") c.configure(state=DISABLED) return "break" print("+i+",i) if __name__ == '__main__': root = Tk() frames = [] table = [] ind = [] ind1 = [] i = 1 c = Button(root, text="add label", command=addNewLabel) c.grid(column=0, row=0) root.mainloop()thank you for your time