Jump to content

automatically generates a frame


Recommended Posts

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.

 

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()

 

Link to comment
Share on other sites


First, you missed the information which programming language you use. This looks like Python, but which version?

Second, if you translate comments to English, there might be many more people in here able to help you.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...