它调用的函数完成后如何关闭Toplevel窗口? [英] How to close Toplevel window after the function it calls completes?

查看:78
本文介绍了它调用的函数完成后如何关闭Toplevel窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我包括我的代码,以便我可以获得一些具体的帮助.

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

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

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?

推荐答案

@Kosig 它不会退出 root.IE.self.foo = tk.Toplevel(self) 然后 self.foo.destroy()

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

例如:

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

您有一个主要对象,即 Foo.Foo 有一个主窗口(称为框架"),它来自tk.Frame.之后,必须在其中创建所有顶层窗口(框架).所以,这里的新窗口是 self.window_bar 并且它的所有对象"都在那里,包括销毁它的方法(self.window_bar.destroy()).您可以从代码的任何部分调用 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_bar and 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".

这篇关于它调用的函数完成后如何关闭Toplevel窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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