删除所有子组件后如何添加子组件?

How to add child components after deleting all child components?

目前,我正在执行以下代码来删除 gui 上的子小部件

for child in infoFrame.winfo_children(): child.destroy()

但是,gui 不会向 gui 添加另一个子项。例如,下面代码的两行

people.place(in_ = gui, x = 1, y = 1, width = 422, height = 449)

people.grid(row = 0, column = 0)

在图形用户界面上放置一个标签。我正在使用以下代码创建标签

people = Label(text = "Default", fg = "black", bg = "white")

编辑 我被要求添加我的 gui 代码,所以这里是:

def initializeGui(name = "Default"):
    GUI = Tk()
    GUI.geometry("423x450+200+200")
    GUI.title(name)
    return GUI

def buttonAnswers(): #This is what I'm focusing on
    gui.title("Answers")
    for child in gui.winfo_children():
        child.destroy()
    return True
    people = Label(text = "Default", fg = "black", bg = "white")
    #people.place(in_ = gui, x = 1, y = 1, width = 422, height = 449)
    people.grid(row = 0, column = 0)

def buttonTest(): #This will be the same as the button above but will open a different gui
    gui.title("Test")
    for child in gui.winfo_children():
        child.destroy()
    return True

    question = Label(text = "Do you want to see the Answers or take the Test?", fg = "black", bg = "white")
    question.grid(row = 0, column = 1)
    checkAns = Button(gui, text = "Answers", command = buttonAnswers, fg = "black", width=10)
    checkAns.grid(row = 1, column = 0)    

gui = initializeGui("School Test")
label = Label(text = "Do you want to see the Answers or take the Test?", fg = "black", bg = "white")
label.grid(row = 0, column = 1)
answers = Button(gui, text = "Answers", command = buttonAnswers, fg = "black", width=10)
questions = Button(gui, text = "Test", command = buttonTest, fg = "black", width=10)
answers.grid(row = 1, column = 0)
questions.grid(row = 1, column = 2)`

问题的解决方案如下:

def buttonAnswers(): #This is what I'm focusing on
    gui.title("Answers")
    for child in gui.winfo_children():
        child.destroy()
    return True
    people = Label(text = "Default", fg = "black", bg = "white")
    #people.place(in_ = gui, x = 1, y = 1, width = 422, height = 449)
    people.grid(row = 0, column = 0)

在 for 循环下面包含一个 return True,阻止程序继续。因此,删除 return True 允许程序继续脚本并将其他子项添加到表单中。

def buttonAnswers(): #This is what I'm focusing on
    gui.title("Answers")
    for child in gui.winfo_children():
        child.destroy()
    people = Label(text = "Default", fg = "black", bg = "white")
    #people.place(in_ = gui, x = 1, y = 1, width = 422, height = 449)
    people.grid(row = 0, column = 0)

在销毁小部件之后但在添加任何新小部件之前,您有一个 return 语句。添加新小部件的代码永远不会执行。