I am trying to delete a window using a button.

时间:2017-04-10 02:45:51

标签: python tkinter

I am trying to delete the previous window I created but once I use a command to call it, I receive an error. Can anyone care to explain how this works?

from tkinter import  *
root= Tk()

I need help here.

def clickEvent():

    master = Tk()
    Label2=Label(master, text="Here are the scientific names of some Animals", font=("Times", 13) ).grid(row=0)
    Label(master, text="Wolf=Canis lupus", font=("Arial", 11)).grid(row=1)
    Label(master, text="Lion=Panthera leo",font=("Arial", 11)).grid(row=2)
    Label(master, text="Panda=Ailuropoda melanoleuca", font=("Arial", 11)).grid(row=3)
    Label(master, text="Jellyfish=Medusozoa", font=("Arial", 11)).grid(row=4)
    Label(master, text="Marmoset=Callithrix jacchus", font=("Arial", 11)).grid(row=5)
    Label(master, text="Tiger=Panthera tigris", font=("Arial", 11)).grid(row=6)
    Label(master, text="Zebra=Equus quagga", font=("Arial", 11)).grid(row=7)
    Label(master, text="Would you like to take the quiz now?", font=("Arial", 13)).grid(row=15)
    e1 = Entry(master,textvariable=entryText).grid(row=1, column=1)
    button2 = Button(root, text="Yes", command=create_window)
    button2.pack()


mylabel=Label(root, text="Welcome to the Animal Trivia Game!", font=("Arial", 14))
mylabel.pack()
label2=Label(root, text="Click 'Start' to begin:)", font=("Arial", 14))
label2.pack()
topFrame=Frame(root)
topFrame.pack()
bottomFrame=Frame(root)
bottomFrame.pack(side=BOTTOM)
leftFrame=Frame(root)
leftFrame.pack(side=LEFT)
rightFrame=Frame(root)
rightFrame.pack(side=RIGHT)




button1=Button(topFrame,text="Start", font=("Arial", 16), fg="White",bg="Turquoise",height=1, width=10,command=clickEvent)
button1.pack()

mainloop()

def clickEvent2():
    master=Tk()

    mainloop()







def clickEvent2():
    master = Tk()
    topFrame=Frame(root)
    topFrame.pack()
    bottomFrame=Frame(root)
    bottomFrame.pack(side=BOTTOM)
    leftFrame=Frame(root)
    leftFrame.pack(side=LEFT)
    rightFrame=Frame(root)
    rightFrame.pack(side=RIGHT)

button2=Button(topFrame,text="Start Now", font=("Arial", 16), fg="White",bg="Turquoise",height=1, width=10,command=clickEvent)
button2.pack()

mainloop()



root.mainloop()

greatly appreciate it if anyone could help.

1 个答案:

答案 0 :(得分:1)

You should never have more than one Tk and Mainloop, it will cause many problems in your code. You will have to use a Toplevel instead, which is basically also a new window. To delete a Toplevel, use it's .destroy() method. While to hide it, use it's .withdraw() method.