How do I create a popup window in Tkinter?

TkinterServer Side ProgrammingProgramming

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 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

Run the above code to display the output which contains a button and a Label Text.

When we click the "Open" button, it will open the popup window (top-level window) on the screen.

raja
Published on 03-May-2021 09:05:23
Advertisements