How do I create a popup window using Tkinter?


Tkinter supports toplevel classes, and these classes contain toplevel window. Toplevel window is also known as child window. We can create a toplevel window by creating the object of Toplevel(parent).

The toplevel window inherits all the properties of Tkinter's parent object. It can contain widgets, frames, canvas and other objects as well.


In this example, we will create a button that will open a popup window.

#Import the required libraries
from tkinter import *

#Create an instance of Tkinter Frame
win = Tk()

#Set the geometry

def open_win():
   #Create a Button to Open the Toplevel Window
   top= Toplevel(win)
   top.title("Child Window")
   #Create a label in Toplevel window
   Label(top, text= "Hello World!")

Label(win, text= "Click the button to Open Popup Window", font= ('Helvetica 18')).place(relx=.5, rely=.5, anchor= CENTER)
Button(win, text= "Click Me", background= "white", foreground= "blue", font= ('Helvetica 13 bold'), command= open_win).pack(pady= 50)


Running the above code will display a window with a Label and a button.

Now, clicking the button will open a New Popup window.

Updated on 25-May-2021 09:14:19