How to open a new window by the user pressing a button in a tkinter GUI?


Tkinter creates a default window (i.e., master or root window) for every application. In tkinter, we can create a Popup window or a child window by defining a Toplevel(master) constructor. This will allow the tkinter application to create another window which can be resized dynamically by defining its size property.

Example

In this example, we have created a button widget that will open the new window with a text label.

#Import tkinter library
from tkinter import *
from tkinter import ttk
#Create an instance of tkinter frame or window
win= Tk()
#Set the geometry of tkinter frame
win.geometry("750x250")
#Define a new function to open the window
def open_win():
   new= Toplevel(win)
   new.geometry("750x250")
   new.title("New Window")
   #Create a Label in New window
   Label(new, text="Hey, Howdy?", font=('Helvetica 17 bold')).pack(pady=30)
#Create a label
Label(win, text= "Click the below button to Open a New Window", font= ('Helvetica 17 bold')).pack(pady=30)
#Create a button to open a New Window
ttk.Button(win, text="Open", command=open_win).pack()
win.mainloop()

Output

Running the above code will display a window that contains a button widget. When we click the button, it will open a new Window.

Now, click the "Open" button to open a new Window.

Updated on: 22-Apr-2021

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements