How do you close all Tkinter windows when specific window closes?

旧巷老猫 提交于 2021-01-28 10:24:35

问题


I have this application in Python Tkinter. There is a Python file which is a main menu. When I click an option in the main menu it imports a python file with code that makes a new window (couldn't use Toplevel for the new window for some reasons). So when I close the main menu it should close all the other windows.

Here is my code for the main menu:

from tkinter import *


root = Tk()
root.geometry("600x600")


def newWindowImport():
    import file1

def newWindowImport2():
    import file2


newWindow = Button(text="new window", command=newWindowImport).pack()
newWindow2 = Button(text="new window", command=newWindowImport2).pack()


# Here is there a way so that when I exit it destroys the Main Menu as well as the opened windows
exitBtn = Button(text="Exit", command=root.destroy())


root.mainloop()

I tried the root.destroy method but it only destroys the main menu and not all the windows. Is there a way so that when I exit the main menu it destroys the main menu as well as the opened windows? If I were to use Toplevel - how would I use it in a separate file?


回答1:


I am assuming that your other scripts have individual instances of Tk(), their own mainloop() and are not under a function, if that is the case, you can have all the code in your files under a function and use Toplevel(), example, file1 should look like

def something():
    window=Toplevel()
    #Rest of the code

And similarly file2, after that in your main program you could do something like this

from tkinter import *
import file1, file2

root = Tk()
root.geometry("600x600")

def newWindowImport():
    file1.something()

def newWindowImport2():
    file2.something()

newWindow = Button(text="new window", command=newWindowImport)
newWindow.pack()
newWindow2 = Button(text="new window", command=newWindowImport2)
newWindow2.pack()

# Here is there a way so that when I exit it destroys the Main Menu as well as the opened windows
exitBtn = Button(text="Exit", command=root.destroy)

root.mainloop()

You could also let go of the functions and make these changes to have it shorter

newWindow = Button(text="new window", command=file1.something)
newWindow.pack()
newWindow2 = Button(text="new window", command=file2.something)
newWindow2.pack()

The reason for your approach not working would be that each file had it's own mainloop() and hence they couldn't be destroyed when you called root.destroy in the main code.

Also note that I have removed the parentheses () from the command=root.destroy otherwise the it will be called as soon as the program initializes.

EDIT : As also suggested by @martineau in the comments, it's better to use .pack() on the Button instances separately as it provides more flexibility in using the instance later in the program, as opposed to having them hold the value None which is the return from .pack()




回答2:


Using Toplevel is the correct way to do this, you need to find out why that is not working and correct it. If you did that this question would solve itself. Also, you need to remove the () from the command, it should be like this:

exitBtn = Button(text="Exit", command=root.destroy)


来源:https://stackoverflow.com/questions/65526200/how-do-you-close-all-tkinter-windows-when-specific-window-closes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!