当特定窗口关闭时,如何关闭所有Tkinter窗口? [英] How do you close all Tkinter windows when specific window closes?

查看:84
本文介绍了当特定窗口关闭时,如何关闭所有Tkinter窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python Tkinter中有此应用程序.有一个Python文件,它是一个主菜单.当我在主菜单中单击一个选项时,它会导入一个带有创建新窗口的代码的python文件(由于某些原因,无法将 Toplevel 用于新窗口).因此,当我关闭主菜单时,应该关闭所有其他窗口.

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.

这是我主菜单的代码:

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()

我尝试了 root.destroy 方法,但它只会破坏主菜单,而不会破坏所有窗口.有没有一种方法可以使我退出主菜单时破坏主菜单以及打开的窗口?如果我要使用 Toplevel -如何在单独的文件中使用它?

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?

推荐答案

我假设您的其他脚本具有 Tk()的单独实例,它们自己的 mainloop()> 并且不在函数下,如果是这种情况,您可以将文件中的所有代码放在函数下并使用 Toplevel(),例如,file1 应该看起来像

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

以及类似的 file2 ,之后在主程序中您可以执行以下操作

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()

您的方法不起作用的原因是每个文件都有自己的 mainloop(),因此当您在其中调用 root.destroy 时,它们无法被销毁主要代码.

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.

还请注意,我已经从 command = root.destroy 中删除了括号(),否则程序初始化后将立即调用它.

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.

正如@martineau在评论中所建议的那样,最好在 Button 实例上单独使用 .pack()与让它们保留值 None (这是 .pack()

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()

这篇关于当特定窗口关闭时,如何关闭所有Tkinter窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆