4

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?

Kosig
  • 201
  • 3
  • 5
  • 14

3 Answers3

8

@Kosig It won't quit root. Ie. self.foo = tk.Toplevel(self) and then 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_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".

HexaGridBrain
  • 522
  • 7
  • 24
marw
  • 2,939
  • 4
  • 28
  • 37
  • But how do I time it to destroy after the function completes? – Kosig Feb 10 '11 at 02:28
  • You don't time anything. You change your "go" button so it calls another function that calls your `bounceProg` function, _then_ call `destroy` on your window. – user336851 Feb 10 '11 at 02:41
  • @Kosig You don't time it, as already pointed out. You have to be careful about the order of function/method calls. The `.destroy()` can be called form any part of your code, so you can close it just before you create another window. However, in case you are using threads, you have to make sure the thread finishes before the windows closes. – marw Feb 10 '11 at 10:46
  • I for some reason am having a real block here...So I have an "are you sure" window that opens when I select "go" from the homescreen. Then when I select "yes" in the are you sure window I want the whole are you sure window to close after the function that is called from the "yes" selection finishes. I have the function defined and separately I have the window defined with a button whose command is the function. Where do I want to try and add this code? Inside the function description says "global var not found" and inside the are you sure window definition closes it right after opening. – Kosig Feb 10 '11 at 20:45
  • You "try" the code from the object/method that created the Toplevel frame. In the example that is `self.window_bar`. – marw Feb 10 '11 at 22:59
5

If you create a toplevel window with the Toplevel command, you destroy it with the destroy method of the window object. For example:

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()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • is imperative to use classes to create or destroy a toplevel window or I can use a simple def function to do something like that? – Diego May 23 '17 at 16:47
  • @diego: You can use a simple function. – Bryan Oakley May 23 '17 at 16:51
  • How can I do that, I have a button that call a function that opens a toplevel for user authentication and when I press the accept button I want to close that toplevel window but don`t know how to call It so I can use the method destroy. – Diego May 23 '17 at 16:55
  • @diego: stackoverflow isn't designed for discussions. If you have a question, click the "Ask Question" button. – Bryan Oakley May 23 '17 at 16:58
0

Apparently you just call quit on the root object that's running your 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

Community
  • 1
  • 1
Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115