Home > front end >  Tkinter python not terminating the program
Tkinter python not terminating the program

Time:11-28

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?

CodePudding user response:

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?

CodePudding user response:

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.

  • Related