English 中文(简体)
如何在调用的函数完成后关闭Toplevel窗口?
原标题:How to close Toplevel window after the function it calls completes?

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

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

最佳回答

@科西格它不会放弃根。Ie.self.foo=tk.顶级(self),然后self.foo.dedestroy()

例如:

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.dedestroy())。您可以从代码的任何部分调用self.window_bar.deDestroy((),但这里是在用户单击“否”后调用的。

问题回答

如果使用toplevel命令创建顶级窗口,则使用窗口对象的destroy方法将其销毁。例如:

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

显然,您只是在运行主循环的根对象上调用quit

编辑:所有Tkinter小部件都有destroy()方法,该方法会破坏该小部件及其子部件。因此,您应该能够在Toplevel上调用此方法





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...