Bugsy
Bugsy

Reputation: 55

How do you close a tkinter window in another function?

I want a button in my window to open a new window and close the previous one. Is it possible to have one button do both of these? I've tried in the following code, but it hasn't worked, just told me that window is not defined:

import tkinter
def window1():
    window = tkinter.Tk()
    tkinter.Button(window, text = "Next", command = window2).pack()
    window.mainloop()
def window2():
    window.destroy() #This is where the error is
    menu = tkinter.Tk()
    etc, etc, etc
window1()

Upvotes: 2

Views: 9369

Answers (4)

endorpheus
endorpheus

Reputation: 121

using Python3
You could use a "global" such as:

root = Tk()
root.title('This is the root window')

def window_create():
    global window_one
    window_one = Tk()
    window_one.title('This is window 1')

Then, from any function (or elsewhere) when you want to destroy window_one, do:

def window_destroyer():
    window_one.destroy()

You could call your window_destroyer function from a button anywhere such as root which the example shows:

kill_window_btn = Button(root, text="Destroy", command=window_destroyer).pack()

Of course, follow your own naming conventions. :)
It seems to me, just 'global window_one' would solve it.

Upvotes: 0

user9204722
user9204722

Reputation:

Yes. Is possible. But you'll need to def that:

def window1:
   blablabla
   blablabla
def window2:
   window2.destroy() <-- Here where the error was

How you noticed, put your name of window what you want Destroy and it will work!

Upvotes: 0

Munir
Munir

Reputation: 3612

First, you need to return the window object from the first function:

def window1():
    window = tkinter.Tk()
    tkinter.Button(window, text = "Next", command = lambda: window2(window)).pack()
    window.mainloop()
    return window

Then, you need to pass the window as an argument to your function:

def window2(window):
    window.destroy()
    menu = tkinter.Tk()

And then call window1 with:

window = window1()

and click the button to destroy it and do the rest

Upvotes: 3

user4171906
user4171906

Reputation:

This is an example using Toplevels, which is usually a better choice than creating, destroying, re-creating Tk() instances. The unique Toplevel ID is passed to the close_it function using partial(). You would, of course, combine them or have the close function call the open function.

try:
    import Tkinter as tk     ## Python 2.x
except ImportError:
    import tkinter as tk     ## Python 3.x

from functools import partial

class OpenToplevels():
    """ open and close additional Toplevels with a button
    """
    def __init__(self):
        self.root = tk.Tk()
        self.button_ctr=0
        but=tk.Button(self.root, text="Open a Toplevel",
                      command=self.open_another)
        but.grid(row=0, column=0)
        tk.Button(self.root, text="Exit Tkinter", bg="red",
                  command=self.root.quit).grid(row=1, column=0, sticky="we")
        self.root.mainloop()

    def close_it(self, id):
        id.destroy()

    def open_another(self):
        self.button_ctr += 1
        id = tk.Toplevel(self.root)
        id.title("Toplevel #%d" % (self.button_ctr))
        tk.Button(id, text="Close Toplevel #%d" % (self.button_ctr),
                  command=partial(self.close_it, id),
                  bg="orange", width=20).grid(row=1, column=0)

Ot=OpenToplevels()

Upvotes: 1

Related Questions