Create a New Window by Clicking a Button in Tkinter

In this tutorial, we will show you how to create and open a new Tkinter window by clicking a button in Tkinter.

Create a New Tkinter Window

import tkinter as tk

def createNewWindow():
    newWindow = tk.Toplevel(app)

app = tk.Tk()
buttonExample = tk.Button(app, 
              text="Create new window",
              command=createNewWindow)
buttonExample.pack()

app.mainloop()

We normally use tk.Tk() to create a new Tkinter window, but it is not valid if we have already created a root window as shown in the above codes.

Toplevel is the right widget in this circumstance as the Toplevel widget is intended to display extra pop-up windows.

buttonExample = tk.Button(app, 
              text="Create new window",
              command=createNewWindow)

It binds the createNewWindow function to the button.

The new window is an empty window in the above example and you could add more widgets to it just like adding widgets in a normal root window, but need to change the parent widget to the created Toplevel window.

import tkinter as tk

def createNewWindow():
    newWindow = tk.Toplevel(app)
    labelExample = tk.Label(newWindow, text = "New Window")
    buttonExample = tk.Button(newWindow, text = "New Window button")

    labelExample.pack()
    buttonExample.pack()

app = tk.Tk()
buttonExample = tk.Button(app, 
              text="Create new window",
              command=createNewWindow)
buttonExample.pack()

app.mainloop()

As you could see, labelExample and buttonExample have their parent widget as newWindow but not app.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Tkinter Button

  • Change Tkinter Button Color
  • Change the Tkinter Button Size