How do I make a pop-up in Tkinter when a button is clicked?


Popup window in Tkinter can be created by defining the Toplevel(win) window. A Toplevel window manages to create a child window along with the parent window. It always opens above all the other windows defined in any application. We can create a top-level window or a child window by initializing the object of Toplevel(parent). It inherits all the property of its parent window, such as geometry, title, and width or height.

Example

In this example, we will create a button that will open a Popup window above all the other windows.

#Import the required Libraries
from tkinter import *
from tkinter import ttk
#Create an instance of Tkinter frame
win = Tk()
#Set the geometry of Tkinter frame
win.geometry("750x270")

def open_popup():
   top= Toplevel(win)
   top.geometry("750x250")
   top.title("Child Window")
   Label(top, text= "Hello World!", font=('Mistral 18 bold')).place(x=150,y= 80)

Label(win, text=" Click the Below Button to Open the Popup Window", font=('Helvetica 14 bold')).pack(pady=20)
#Create a button in the main Window to open the popup
ttk.Button(win, text= "Open", command= open_popup).pack()
win.mainloop()

Output

Running the above code will display a window that will have a button to open the Popup Window.

Now, click the Open button to Open the Popup window. Once we click the "Open" button, a popup window will appear on the screen.

Updated on: 03-May-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements