简体   繁体   中英

Tkinter button not destroying labels under certain conditions

Bit of a long one apologies

Basically, the main issue is that the 'add_button3' (back to homepage button) doesn't work if you press 'add_button2' (new question) before, it takes you to the homepage but doesn't destroy any of the labels. Whereas if you submit a question and the mark is still visible and then press 'add_button3' it destroys all the labels and shows the homepage with no problem. Is there an issue with the indents or anything that is causing this? I have attached a segment of the relevant code below but the entire project can be found at https://github.com/wright-jake/math-game

Big kudos to anyone who can help me out!

def addition():

  #select random numbers
  num_1 = randint(0,30)
  num_2 = randint(0,30)

  #displays question
  add_label1 = Label(root, text=f"What is {num_1} + {num_2} ?\n",font=('Times_New_Roman',16))
  add_label1.place(height=30,x=190,y=75)

  #provide answer in entry box
  add_selection = Entry(root)
  add_selection.place(height=30,x=190,y=120)

  #accept answer and decide if it is right or wrong
  def add_submt():

    #correct
    if int(add_selection.get()) == num_1 + num_2:
      mark = Label(root,text="Correct! Keep on playing!\n",font=('Times_New_Roman',16))
      mark.place(height=30,x=160,y=200)

    #incorrect
    else:
      mark = Label(root,text=f"Incorrect, the answer is {num_1+num_2}.\n",font=('Times_New_Roman',16))
      mark.place(height=30,x=150,y=200)

    #new question will destroy old question and loop the addition function again
    def new_question():
      addition()
      mark.destroy()

    #back to homepage destroys current page
    def add_back():
      add_label1.destroy()
      add_button1.destroy()
      add_button2.destroy()
      add_selection.destroy()
      add_button3.destroy()
      mark.destroy()
      homepage()

    #new question button
    add_button2 = Button(root,text="New Question",command=new_question)
    add_button2.place(height=30,x=190,y=240)

    #back to homepage button
    add_button3 = Button(root,text="Back",command=add_back)
    add_button3.place(height=30,x=340,y=240)

  #submit answer
  add_button1 = Button(root,text="Submit",command=add_submt)
  add_button1.place(height=30,x=190,y=160)

What I recommend you to do is build frames for your pages, in that way you can just add each of your buttons for example to your frame, and if you want to go from a question page you just do eg (question_frame.destroy()) and all the buttons and labels in this frame will be destroyed and you can call your homepage function. Then you just setup your homepage in another frame and so on... Example:

homepage_frame = Frame(root)

homepage_frame.pack()

l1 = label(homepage_frame...) l1.pack()

Just put this in a function and repeat it for all of your "windows".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM