0

这是我的函数,它生成 4 个具有不同值的按钮,但我想要的是销毁之前创建的按钮。

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

谢谢。

4

1 回答 1

1

您可以简单地使用列表来存储创建的按钮。然后您可以通过列表销毁按钮,清除列表,然后将这些新按钮添加到其中。

# 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])
于 2021-11-17T13:25:04.083 回答