How to create multiple windows in Tkinter with a button

How to create multiple windows in Tkinter with a button

create multiple windows in Tkinter with button

Multiple windows using python and Tkinter

Today I will walk through to you how you can create multiple windows in Tkinter with a button.

First, we write a simple bare-bones code.

from tkinter import * 

root = Tk()
root.title('Tkinter GUI - Yourname')
root.geometry('400x400')


root.mainloop()

After that, we are going to create a button. When we click the button the new window should pop-up

#Button to create new window when it clicked 
my_button = Button(root, text="Create New Window", command=new_window) 
my_button.pack()

Note that in the button I wrote a command=new_window This is nothing but the command that triggers a function to create a new window.

So far so good. Now we are going to create a function which able to create a new separate window. Write below function before my_button line, otherwise it will through an error.

#This function is able to create new separate window
def new_window():
    new_root = Tk()
    new_root.title("New Tkinter Window")
    new_root.geometry("500x500")

That's it. Congratulation you made it.

See how your final code looks like:

from tkinter import * 

root = Tk()
root.title('Tkinter GUI - Sailendra')
root.geometry('400x400')

def new_window():
    new_root = Tk()
    new_root.title("New Tkinter Window")
    new_root.geometry("500x500") 

my_button = Button(root, text="Create New Window", command=new_window) 
my_button.pack() 

root.mainloop()

If you feel difficulty or I made any mistake, then please reach out to me at Twitter

Happy Coding :)