Edward Nivison
Edward Nivison

Reputation: 15

How do I close a Tkinter Window when a new Tkinter window opens?

I would like to know how I can open a tkinter window using python, and then later on in the script, it is closed to open another tkinter window. In the windows I am planning to just put images in so that it is updating whenever the script needs it too. I am making a text-based game where you choose the direction you want to go and am using the tkinter window to display the map. If there is any better ways of doing this, that would be greatly appreciated, but I am happy with having a tkinter window close and open when a new window is called.

Upvotes: 0

Views: 3588

Answers (1)

furas
furas

Reputation: 142641

Create empty window and run mainloop() (event loop) to keep it open

import tkinter as tk

window = tk.Tk()
window.mainloop()

To close it you can use standard button [x] on title bar.

Or you would need to put button in window which runs window.destroy() to close it.

import tkinter as tk

window = tk.Tk()

button = tk.Button(window, text='CLOSE', command=window.destroy)
button.pack()

window.mainloop()

BTW: in command= it has to be window.destroy without (). It is "callback" - name of function which will be executed when you press button.


In this example button runs function which closes first window and create second window.

import tkinter as tk

# --- functions ---

def second_window():
    window.destroy()

    second = tk.Tk()

    button = tk.Button(second, text='CLOSE SECOND', command=second.destroy)
    button.pack()

    second.mainloop()

# --- main ---

window = tk.Tk()

button = tk.Button(window, text='CLOSE FIRST', command=second_window)
button.pack()

window.mainloop()

Previous version opens second window only if you use tk.Button() but it doesn't do it when you press [x]. You need

window.protocol("WM_DELETE_WINDOW", second_window)

to run second_window when you press [x]

import tkinter as tk

# --- functions ---

def second_window():
    window.destroy()

    second = tk.Tk()

    button = tk.Button(second, text='CLOSE SECOND', command=second.destroy)
    button.pack()

    second.mainloop()

# --- main ---

window = tk.Tk()

button = tk.Button(window, text='CLOSE FIRST', command=second_window)
button.pack()

window.protocol("WM_DELETE_WINDOW", second_window)

window.mainloop()

You can even run code which close and create again the same window

import tkinter as tk

# --- functions ---

def first_window():
    global window

    if window:
        window.destroy()
        #window = None 

    window = tk.Tk()

    button = tk.Button(window, text='CLOSE FIRST', command=first_window)
    button.pack()

    window.protocol("WM_DELETE_WINDOW", first_window)

    window.mainloop()

# --- main ---

window = None
first_window()

In this example I use window = None to check if window exists and close it. You can use this method to close first window when you create new window.


In this example I use first_window = None and second_window = None so I can check if other window is open and close it.

import tkinter as tk

# --- functions ---

def open_second_window():
    global first_window
    global second_window

    if first_window:
        first_window.destroy()
        first_window = None

    second_window = tk.Tk()

    button = tk.Button(second_window, text='OPEN FIRST', command=open_first_window)
    button.pack()

    second_window.protocol("WM_DELETE_WINDOW", open_first_window)

    second_window.mainloop()

def open_first_window():
    global first_window
    global second_window

    if second_window:
        second_window.destroy()
        second_window = None

    first_window = tk.Tk()

    button = tk.Button(first_window, text='OPEN SECOND', command=open_second_window)
    button.pack()

    first_window.protocol("WM_DELETE_WINDOW", open_second_window)

    first_window.mainloop()

# --- main ---

first_window = None
second_window = None
open_first_window()

But I don't know why you what to close one window and open another. You can hide first window if later you want to go back to first window. Or you can keep widgets in Frame() and remove/this frame to show other frame.

Upvotes: 2

Related Questions