Closing current window when opening a new window in tkinter python

8.3k Views Asked by At

I have created a program in Python using tkinter. I have created two seperate classes for two windows. I am opening the other window by clicking on the button of one window. I want it such that when new window opens the other should close. My code is

from tkinter import Tk, Toplevel
from tkinter import *
def main():
    main_window = Tk()
    app = first(main_window)
    main_window.mainloop()


class first:
    def __init__(self, root):
        self.root = root
        self.root.title('First window')

        self.root.geometry('1350x700+0+0')
        frame1 = Frame(self.root, bg='black')
        frame1.place(x=400, y=50, width=400, height=600)
        btn_1 = Button(frame1, command=self.second_window, text='open second window', font=("Times New Roman", 15, 'bold'), bd=3,
                           relief=RIDGE,
                           cursor='hand2', bg='red', fg='white', activeforeground='white', activebackground='red')
        btn_1.place(x=100, y=350, width=220, height=35)

    def second_window(self):
        self.new_window = Toplevel(self.root)
        self.app = second(self.new_window)


class second:
    def __init__(self, root):
        self.root = root
        self.root.title('Second Window')
        self.root.geometry("1350x700+0+0")
        self.root.config(bg='white')
        frame1 = Frame(self.root, bg='black')
        frame1.place(x=400, y=50, width=400, height=600)
        btn_1 = Button(frame1, command=self.first_window, text='open first window',
                       font=("Times New Roman", 15, 'bold'), bd=3,
                       relief=RIDGE,
                       cursor='hand2', bg='red', fg='white', activeforeground='white', activebackground='red')
        btn_1.place(x=100, y=350, width=220, height=35)

    def first_window(self):
        self.new_window = Toplevel(self.root)
        self.app = first(self.new_window)


if __name__ == '__main__':
    main()

I understand this question is quite common but I cant find a soloution on here which would be applicable for my code.

2

There are 2 best solutions below

0
On BEST ANSWER

You can destroy the previous window and start a new window using Tk class Here is an example

from tkinter import *
from tkinter.ttk import Button
root = Tk()
root.title("title")
root.geometry("800x500")

def window2():
    root.destroy()
    window2_main = Tk()
    Label(window2_main, text="Bye Bye").pack()
    window2_main.mainloop()
a = Button(text="Click This", command=window2)
a.pack()
root.mainloop()

0
On

You can try this version:

import tkinter as tk
from tkinter import ttk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry("400x200")
        self.title("window 1")

        # widget
        self.widgets()

    def widgets(self):
        button = ttk.Button(self, text="Press Me", command=self.open_window)
        button.pack(expand=True)

    def open_window(self):
        self.withdraw() 
        top = TopWindow(self)
        top.protocol("WM_DELETE_WINDOW", self.on_top_window_close)
        top.deiconify() 

    def on_top_window_close(self):
        self.deiconify() 
        self.destroy()


class TopWindow(tk.Toplevel):
    def __init__(self, master):
        super().__init__(master)
        self.title("Window 2")
        self.geometry("600x300")


if __name__ == "__main__":
    main = App()
    main.mainloop()