0

Here is my function, it generates 4 buttons with different values, but what I would like is to destroy the buttons previously created.

def bouttonedSeg():
    segments = randSeg()
    b1 = Button(fenetre, text=segments[0], command=lambda:ecrire_segment(segments[0]))
    b1.place(x=100,y=100)
    b2 = Button(fenetre, text=segments[1], command=lambda:ecrire_segment(segments[1]))
    b2.place(x=100,y=120)
    b3 = Button(fenetre, text=segments[2], command=lambda:ecrire_segment(segments[2]))
    b3.place(x=100,y=140)
    b4 = Button(fenetre, text=segments[3], command=lambda:ecrire_segment(segments[3]))
    b4.place(x=100,y=160)
    pc.copy(segments[0] + ";" + segments[1] + ";" + segments[2] + ";" + segments[3])

Thanks.

acw1668
  • 40,144
  • 5
  • 22
  • 34

1 Answers1

1

You can simply use a list to store the created buttons. Then you can go through the list to destroy the buttons, clear the list and then add those new buttons to it.

# list to store created buttons
buttons = []

def bouttonedSeg():
    # destroy buttons previously created
    for b in buttons:
        b.destroy()
    # clear the list
    buttons.clear()

    segments = randSeg()
    b1 = Button(fenetre, text=segments[0], command=lambda:ecrire_segment(segments[0]))
    b1.place(x=100,y=100)
    b2 = Button(fenetre, text=segments[1], command=lambda:ecrire_segment(segments[1]))
    b2.place(x=100,y=120)
    b3 = Button(fenetre, text=segments[2], command=lambda:ecrire_segment(segments[2]))
    b3.place(x=100,y=140)
    b4 = Button(fenetre, text=segments[3], command=lambda:ecrire_segment(segments[3]))
    b4.place(x=100,y=160)
    pc.copy(segments[0] + ";" + segments[1] + ";" + segments[2] + ";" + segments[3])

    # save the created buttons
    buttons.extend([b1, b2, b3, b4])
acw1668
  • 40,144
  • 5
  • 22
  • 34