0
import tkinter

global win1,win2, win3

def win1_open():
    global win1
    win1 = tkinter.Tk()
    win1.geometry('500x500')
    button_next = tkinter.Button(win1, text='Next', width=8, command=win2_open)
    button_next.place(x=100 * 2 + 80, y = 100)
    win1.mainloop()

def win2_open():
    global win2
    win2 = tkinter.Tk()
    win2.geometry('500x500')
    button_next = tkinter.Button(win2, text='Next', width=8, command=win3_open)
    button_next.place(x=100 * 2 + 80, y=100)
    win2.mainloop()

def win3_open():
    global win3
    win3 = tkinter.Tk()
    win3.geometry('500x500')
    button_exit = tkinter.Button(win3, text='Exit', width=8, command=exit_program)
    button_exit.place(x=100 * 2 + 80, y=100)
    win3.mainloop()

def exit_program():
    global win1, win2, win3
    win1.quit()
    win2.quit()
    win3.quit()

win1_open()


The third window has Exit button that I have used to terminate the program. It terminates the program but only after I click Exit button thrice. How to terminate the program on one button click?

2
  • win.destroy() is what you should be using Commented Nov 27, 2022 at 9:43
  • 1
    You should only have 1 .mainloop() in your code as it can handle the event loop for all tk.Tk() windows. Commented Nov 27, 2022 at 10:24

2 Answers 2

1

Instead of using quit(), you should use destory()

def exit_program():
    global win1, win2, win3
    win1.destroy()
    win2.destroy()
    win3.destroy()

To learn more about the difference between these 2 function, you can refer to this: How do I close a tkinter Window?

Sign up to request clarification or add additional context in comments.

Comments

1

Change the calls in exit_program() to .destroy() instead of .quit()

Also, it's not necessary to have more than one .mainloop() in your Tkinter program. One mainloop suffices.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.