Computer >> Computer tutorials >  >> Programming >> Python

How do I create a popup window using Tkinter Program?


Tkinter has many inbuilt functions and features which can be used to extend the internal functionality of an application. Popups in Tkinter are created by defining the messagebox. In order to work with the popup message boxes, you have first to import the messagebox package in Tkinter by the command "import tkinter.messagebox".

Example

In this example, we will create a messagebox popup with a question. On clicking a particular option, it will redirect the user to the respective operation.

# Import the required libraries
from tkinter import *
import tkinter.messagebox

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

# Set the geometry of Tkinter Frame
win.geometry("700x350")

def open_win():
   out = tkinter.messagebox.askquestion('Prompt', 'Do you want to Continue?')
   if out == 'yes':
      Label(win, text="Thank You for your Response!", font=('Helvetica 22 bold')).pack(pady=40)
   else:
      win.destroy()

# Create a Button
button = Button(win, text="Click Me", command=open_win, font=('Helvetica 14 bold'), foreground='OrangeRed3',background="white")
button.pack(pady=50)
win.mainloop()

Output

On executing the above code, it will display the following window −

How do I create a popup window using Tkinter Program?

Now, click the "Click Me" button. It will show a messagebox with a question.

How do I create a popup window using Tkinter Program?

Next, click the "Yes" button on the messagebox. It will display the following window −

How do I create a popup window using Tkinter Program?