Python 它调用的函数完成后如何关闭Toplevel窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4950359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 18:18:28  来源:igfitidea点击:

How to close Toplevel window after the function it calls completes?

pythontkinter

提问by Kosig

Edit: let me include my code so I can get some specific help.

编辑:让我包含我的代码,以便我可以获得一些特定的帮助。

import Tkinter

def goPush():
    win2=Tkinter.Toplevel()
    win2.geometry('400x50')
    Tkinter.Label(win2,text="If you have prepared as Help describes select Go otherwise select Go Back").pack()
    Tkinter.Button(win2,text="Go",command=bounceProg).pack(side=Tkinter.RIGHT,padx=5)
    Tkinter.Button(win2, text="Go Back", command=win2.destroy).pack(side=Tkinter.RIGHT)

def bounceProg():
    d=1
    print d
root=Tkinter.Tk()
root.geometry('500x100')
Tkinter.Button(text='Go', command=goPush).pack(side=Tkinter.RIGHT,ipadx=50)
root.mainloop()

So when you run the program it opens a window that says Go. Then Go opens a window that asks if youve read the help(which I didnt include in this code sample) and offers Go Back(which goes back) and Go. When you select Go it calls a function which prints 1. After it prints 1 I want the Window to close returning to the original window containing the Go button. How do I do such a thing?

因此,当您运行该程序时,它会打开一个显示 Go 的窗口。然后 Go 打开一个窗口,询问您是否已阅读帮助(我没有包含在此代码示例中)并提供 Go Back(返回)和 Go。当您选择 Go 时,它会调用一个打印 1 的函数。在它打印 1 后,我希望窗口关闭,返回到包含 Go 按钮的原始窗口。我怎么做这样的事情?

采纳答案by marw

@Kosig It won't quit root. Ie. self.foo = tk.Toplevel(self)and then self.foo.destroy()

@Kosig 它不会退出 root。IE。self.foo = tk.Toplevel(self)进而self.foo.destroy()

For example:

例如:

class Foo(tk.Frame):
    """Foo example"""

    def __init__(self, master=None):
        """Draw Foo GUI"""
        tk.Frame.__init__(self, master)
        self.grid()
        self.draw_window_bar()

    def draw_window_bar(self):
        """Draw bar TopLevel window"""
        self.window_bar = tk.Toplevel(self)
        # Some uber-pythonian code here...
        ask_yes_or_no = messagebox.askyesno('Brian?', 'Romani Ite Domum')
        if not ask_yes_or_no:
            self.window_bar.destroy()

You have one main object, which is Foo. Foo has one main window (called "frame"), which it gets from tk.Frame. Afterwards, all Toplevel windows (frames) must be created within it. So, your new window here is self.window_barand all its "objects" are in there, including the method for destroying it (self.window_bar.destroy()). You can call self.window_bar.destroy()from any part of the code, but here it is called after the user clicks "no".

您有一个主要对象,即 Foo。Foo 有一个主窗口(称为“框架”),它从tk.Frame. 之后,必须在其中创建所有顶层窗口(框架)。所以,你的新窗口在这里self.window_bar,它的所有“对象”都在那里,包括销毁它的方法(self.window_bar.destroy())。您可以self.window_bar.destroy()从代码的任何部分调用,但这里是在用户单击“否”后调用的。

回答by Daniel DiPaolo

Apparently you just call quiton the root object that's running your mainloop

显然你只是调用quit运行你的根对象mainloop

edit: All Tkinter widgets have a destroy()method which destroys that widget and its children. So you should be able to call this on your Toplevel

编辑:所有 Tkinter 小部件都有一个destroy()方法可以销毁该小部件及其子部件。所以你应该能够在你的 Toplevel 上调用它

回答by Bryan Oakley

If you create a toplevel window with the Toplevelcommand, you destroy it with the destroymethod of the window object. For example:

如果使用该Toplevel命令创建顶层窗口,则使用destroywindow 对象的方法销毁它。例如:

import Tkinter as tk

class MyToplevel(tk.Toplevel):
    def __init__(self, title="hello, world", command=None):
        tk.Toplevel.__init__(self)
        self.wm_title(title)
        button = tk.Button(self, text="OK", command=lambda toplevel=self: command(toplevel))
        button.pack()

if __name__ == "__main__":
    def go(top):
        print "my work here is done"
        top.destroy()

    app = tk.Tk()
    t = MyToplevel(command=go)
    t.wm_deiconify()
    app.mainloop()