How do I create a popup window using Tkinter?

TkinterPythonGUI-Programming

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.

Example

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
win.geometry("700x250")

def open_win():
   #Create a Button to Open the Toplevel Window
   top= Toplevel(win)
   top.geometry("700x250")
   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)
win.mainloop()

Output

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

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

raja
Published on 25-May-2021 09:08:45
Advertisements