How to create a popup window in Tkinter – Python

Hello coders, this tutorial deals with creating a popup window in Tkinter. To learn the basics of the Tkinter module you can refer to this Introduction to Tkinter module.

Create a popup window in Tkinter in Python

Firstly we need to use two import statements i.e Tkinter package to access all GUI tool kit of Tkinter and another one is tkinter.messagebox package under Tkinter for the popup window. Here are the first two statements.

from tkinter import *
import tkinter.messagebox

To get a frame, we need to store that in a variable root(example) by calling Tk() function.

root=Tk()

Now we need to use a method showinfo(‘title of the popup window’, ‘message’). Here the first parameter is for the title of the popup window and the second parameter is for the message to be delivered to the user.

tkinter.messagebox.showinfo('Window Title','This is a popup window')

Finally the closing statement by calling mainloop().

root.mainloop()

So here is the final code for the popup window in Tkinter.

Example 1:

from tkinter import *
import tkinter.messagebox
root=Tk()
tkinter.messagebox.showinfo('Popup Window(Title)','This is a pop up window')
root.mainloop()

Output 1:

create a popup window in Tkinter in Python

Let us take another example where this popup window will prompt for yes or no operation.

Example 2:

from tkinter import * 
import tkinter.messagebox 
root=Tk() 
result=tkinter.messagebox.askquestion('Installation','Do you want to install this anyway?')
if result=='yes':
    theLabel=Label(root,text="Enjoy this software.") #To insert a text
    theLabel.pack()
else:
    root.destroy() #Closing Tkinter window forcefully.
root.mainloop()

Output 2:

create a popup window in Tkinter - Python

This is how the pop window can be created, for any queries please comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *