velpandian
velpandian

Reputation: 471

Destroy window not closing all windows properly

I have two queries in this section.

  1. In my code i have created two frames under root, the first frame have "NEXT" button to go on second frame. In second frame there is Run button, which has mapped with close_window function. It should close all windows properly. But in my case not closing it.

  2. When i click "Run" i need to close all windows and need to execute another script on the same directory. Is that possible to do it ?

 

from Tkinter import *


def close_window():
    frame2.destroy()
    frame1.destroy()


def swap_frame(frame):
    frame.tkraise()


root = Tk()
root.geometry("900x650+220+20")
root.title("Testing")
root.configure(borderwidth="1", relief="sunken", cursor="arrow", background="#dbd8d7", highlightcolor="black")
root.resizable(width=False, height=False)

frame2 = Frame(root, width=900, height=650)
frame1 = Frame(root, width=900, height=650)

Button1 = Button(frame1, text="Next", width=10, height=2, bg="#dbd8d7", command=lambda: swap_frame(frame2))
Button1.place(x=580, y=580)

Button2 = Button(frame2, text="Run", width=10, height=2, bg="#dbd8d7", command=close_window,)
Button2.place(x=580, y=580)


frame2.grid(row=0, column=0)
frame1.grid(row=0, column=0)

root.mainloop()

what is wrong in the code?

Upvotes: 0

Views: 213

Answers (1)

Nae
Nae

Reputation: 15335

"Destroy window not closing all windows properly"

Nor should it. destroy method destroys a widget, and when a widget is destroyed so are its children.

Since neither frame1 nor frame2 are 'windows' or a window's parents, there's no window destruction taking place.


"When I click "Run" I need to close all windows and need to execute another script in the same directory. Is that possible to do it?"

It is possible. Use quit on any GUI object, instead of destroy. It stops the mainloop, hence destroying the entire GUI. In that it solves the first problem as well. Then import another_script:

...
def close_window():
    frame1.quit()
...
root.mainloop()
import another_script

Upvotes: 1

Related Questions