Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

tkinter

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?

like image 408
Kosig Avatar asked Dec 17 '22 17:12

Kosig


2 Answers

@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".

like image 186
Andrew Moll Avatar answered Feb 09 '23 00:02

Andrew Moll


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()
like image 40
Bryan Oakley Avatar answered Feb 09 '23 01:02

Bryan Oakley