Python Tkinter messagebox + Examples

Keep reading to know more about the Python Tkinter messagebox, we will discuss the messagebox in Python Tkinter with examples.

  • Python Tkinter messagebox
  • Python Tkinter messagebox options
  • Python tkinter messagebox size
  • Python tkinter messagebox askquestion
  • Python tkinter messagebox askyesno
  • Python tkinter messagebox position
  • Python tkinter messagebox documentation
  • Python tkinter yes no dialog box
  • Python tkinter yes no message box

If you are new to Python Tkinter or Python GUI programming, check out Python GUI Programming.

Python tkinter messagebox

  • Messagebox is used to display pop-up messages.
  • To get started with message box import a library messagebox in Python.
  • Messagebox provides mainly 6 types of message prompts like showinfo(), showerror(), showwarning(), askquestion(), askokcancel(), askyesno(), askretyrcancel().
showinfo()used when any confirmation/information needs to display. like login successful, message sent, etc.
showerror()used to display error prompt with sound. The ideal time for its appearance is when the user makes any mistakes or skips the necessary step.
showwarning()Shows warning prompt with exclamation sign. It warns the user about the upcoming errors.
askquestion()It asks the user for Yes or No. also it returns ‘Yes‘ or ‘No
askokcancel()It prompts for ‘Ok’ or ‘Cancel‘, returns ‘True‘ or ‘False
askyesno()It prompts for ‘Yes’ or ‘No’. Returns True for ‘Yes’ and False for ‘No’
askyesnocancel()It prompts for ‘Yes’, ‘No’, or ‘Cancel’. Yes returns True, No returns False, and Cancel returns None
askretrycancel()It prompts with Retry and Cancel options. Retry returns True and Cancel returns False.

Python tkinter messagebox options

  • Python Messagebox options are already mentioned in the above point
  • So here we will look at it with an example, we will see examples for showinfo(), showwarning(), askquestion(), askokcancel(), askyesno(), askretrycancel().

Syntax:

from tkinter import messagebox
messagebox.option('title', 'message_to_be_displayed')
python tkinter messagebox syntax
python tkinter messagebox

Code:

Here is the code to represent all kinds of message boxes.

from tkinter import *
from tkinter import messagebox

ws = Tk()
ws.title('Python Guides')
ws.geometry('300x200')
ws.config(bg='#5FB691')

def msg1():
    messagebox.showinfo('information', 'Hi! You got a prompt.')
    messagebox.showerror('error', 'Something went wrong!')
    messagebox.showwarning('warning', 'accept T&C')
    messagebox.askquestion('Ask Question', 'Do you want to continue?')
    messagebox.askokcancel('Ok Cancel', 'Are You sure?')
    messagebox.askyesno('Yes|No', 'Do you want to proceed?')
    messagebox.askretrycancel('retry', 'Failed! want to try again?')

Button(ws, text='Click Me', command=msg1).pack(pady=50)

ws.mainloop()

Output:

Here are the options displayed when Click Me button is clicked.

python tkinter messagebox options
python tkinter messagebox examples

You may like Python Tkinter to Display Data in Textboxes and How to Set Background to be an Image in Python Tkinter.

Python tkinter messagebox size

  • There is no way to change the size of the messagebox.
  • Only more text can stretch the size of messagebox.
  • In this example, I will change the text content to stretch the message box size.

Code:

from tkinter import *
from tkinter import messagebox

ws = Tk()
ws.title('Python Guides')
ws.geometry('400x300')

def clicked():
    messagebox.showinfo('sample 1', 'this is a message')
    messagebox.showinfo('sample 2', 'this is a message to change the size of box')
    messagebox.showinfo('sample 3', ' this is the message that is larger than previous message.')

Button(ws, text='Click here', padx=10, pady=5, command=clicked).pack(pady=20)

ws.mainloop()

Output:

python tkinter messagebox size
Python tkinter messagebox size

Python tkinter messagebox askquestion

  • askquestion prompt is used to ask questions from the user.
  • The response can be collected in the ‘Yes‘ or ‘No‘ form.
  • This function returns the ‘yes‘ or ‘no‘.
  • These return types can be controlled using an if-else statement.
  • We will see this in the below example.

Code:

In this Python code, we have implemented the askquestion messagebox. We stored the value of messagebox in a variable ‘res’. Then we have compared the variable with the return type i.e ‘yes’ or ‘no’.

from tkinter import *
from tkinter import messagebox

ws = Tk()
ws.title('Python Guides')
ws.geometry('300x100')

def askMe():
    res = messagebox.askquestion('askquestion', 'Do you like cats?')
    if res == 'yes':
        messagebox.showinfo('Response', 'You like Cats')
    elif res == 'no':
        messagebox.showinfo('Response', 'You must be a dog fan.')
    else:
        messagebox.showwarning('error', 'Something went wrong!')


Button(ws, text='Click here', padx=10, pady=5, command=askMe).pack(pady=20)

ws.mainloop()

Output:

In this output, there is a button which when clicked will prompt with the question. The user will reply in ‘yes’ or ‘no’. accordingly another messagebox will prompt.

python tkinter messagebox askquestion
Python tkinter messagebox askquestion

Read: Python Tkinter Mainloop

Python tkinter messagebox askyesno

  • askyesno is used to prompt users with ‘yes’ or ‘no’ options.
  • the response can be collected in the ‘yes’ or ‘no’ format.
  • askyesno function returns boolean values i.e. ‘True’ or ‘False’.
  • To control the yes or no use if-else statement.

Code:

from tkinter import *
from tkinter import messagebox
import time

ws = Tk()
ws.title('Python Guides')
ws.geometry('400x200')
ws.config(bg='#345')

def quitWin():
    res = messagebox.askyesno('prompt', 'Do you want to kill this window?') 
    if res == True:
        messagebox.showinfo('countdown', 'killing window in 5 seconds')
        time.sleep(5)
        ws.quit()

    elif res == False:
        pass
    else:
        messagebox.showerror('error', 'something went wrong!')

Button(ws, text='Kill the Window', padx=10, pady=5, command=quitWin).pack(pady=50)

ws.mainloop()

Output:

In this output, the user will see a window with the ‘Kill the Window’ button. Once the user will click on this button.

Python tkinter messagebox askyesno
Python tkinter messagebox askyesno

Python tkinter messagebox position

  • In this section, we will talk about the messagebox position
  • This can be achieved using a Toplevel widget.
  • any other widget can be placed on a Toplevel widget.
  • It is similar to python frames but is specially created for creating customized prompts.
  • In the below example we have placed image, label & 2 buttons.
  • It will look like a prompt but we can position things as per our requirement.
  • for image we have couple of options listed below.
    • ::tk::icons::error
    • ::tk::icons::information
    • ::tk::icons::warning
    • ::tk::icons::question

Code:

Here is a code in Python to create messagebox with customized position of widgets in it. We have placed an icon or image of the question over here. But you can replace it with ‘error’, ‘warning’, ‘question’ or ‘information’

from tkinter import *

def killWin():
	toplevel = Toplevel(ws)

	toplevel.title("Kill window")
	toplevel.geometry("230x100")


	l1=Label(toplevel, image="::tk::icons::question")
	l1.grid(row=0, column=0)
	l2=Label(toplevel,text="Are you sure you want to Quit")
	l2.grid(row=0, column=1, columnspan=3)

	b1=Button(toplevel,text="Yes",command=ws.destroy, width=10)
	b1.grid(row=1, column=1)
	b2=Button(toplevel,text="No",command=toplevel.destroy, width=10)
	b2.grid(row=1, column=2)


ws = Tk()
ws.geometry("300x200")
ws.title('Python Guides')
ws.config(bg='#345')
Button(ws,text="Kill the window",command=killWin).pack(pady=80)

ws.mainloop()

Output:

In this output, the main window has a button which when clicked will prompt a user whether he wants to quit this page or not. If a user says ‘Yes’ this page will disappear’. Since it is a question you can notice a small icon of a question mark on the left side of the prompt.

python tkinter messagebox position
Python tkinter messagebox position

Python tkinter messagebox documentation

Information message box

To display information showinfo function is used.

Warning message box

To display warning or error message showwarning and showerror is used.

Question message box

To ask question there are multiple options:

  • askquestion
  • askokcancel
  • askretrycancel
  • askyesno
  • askyesnocancel

Python tkinter yes no dialog box

Let us see, how to generate Yes or No Popups in Python Tkinter or Python tkinter yes no message box. Python Tkinter provides a messagebox module that allows generating Yes and No prompts.

  • Yes or No Popups are used to ask for confirmation before performing a task. It is mainly used to make sure that the user has not accidentally triggered a task.
  • Yes or No prompts can be seen while the user is about to exit the program or while he is about to initiate any sensitive task. for example, while transferring money you see a prompt “Do you want to Proceed”.
  • There are two functions inside the messagbox module that are used to generate Yes or No Prompts:
    • messagebox.askquestion()
    • messagebox.askyesno()
  • messagebox.askquestion() function is used to ask a question from the user and the user has to reply in yes or no. The function returns True when the user clicks on the Yes button and Flase when the user clicks on the No button.
  • messagebox.askyesno() function also does the same thing that it asks the user to reply in yes or no. The function returns True for yes and False for no.
  • We have a complete section on Python Tkinter messagebox with an example. In this, we have covered all the available options for message box.

Syntax:

  • Here is the syntax of the messagebox in Python Tkinter. In the first line, we have imported the messagebox module from the Tkinter library.
  • In the second line, the option is the function that will generate a prompt. There are seven options to generate the prompt but in this tutorial, we’ll discuss only two of them:
    • messagebox.askquestion()
    • messagebox.askyesno()
from tkinter import messagebox

messagebox.option('title', 'message_to_be_displayed')

Example of Yes No popups in Python Tkinter

  • This application shows two options to the user. One is to donate & another one is to Exit the application.
  • If the user clicks on the transfer button, askquestion() function is triggered & it requests the user for confirmation.
  • if the user clicks on the Exit button, askyesno() function is triggered and it asks the user if he is sure to exit the application.
from tkinter import *
from tkinter import messagebox

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#4a7a8c')

def askQuestion():
    reply = messagebox.askyesno('confirmation', 'Are you sure you want to donate $10000 ?')
    if reply == True:
        messagebox.showinfo('successful','You are the Best!')
    else:
        messagebox.showinfo('', 'Maybe next time!')
       

def askYesNo():
    reply = messagebox.askyesno('confirmation', 'Do you want to quit this application?')
    if reply == True:
        messagebox.showinfo('exiting..', 'exiting application')
        ws.destroy()
    else:
        messagebox.showinfo('', 'Thanks for Staying')
       

btn1 = Button(
    ws,
    text='Transfer',
    command=askQuestion,
    padx=15,
    pady=5
)
btn1.pack(expand=True, side=LEFT)

btn2 = Button(
    ws,
    text='Exit',
    command=askYesNo,
    padx=20,
    pady=5
)
btn2.pack(expand=True, side=RIGHT)

ws.mainloop()

Output of the above code

Here is the output of the above code, In this code user wants to exit the application. He sees Yes No popup after clicking on the exit button.

Python tkinter yes no dialog box
Yes No Popups in Python Tkinter

This is how to generate Yes No Popup in Python Tkinter or Python Tkinter yes no dialog box.

You may like the following Python tutorials:

In this tutorial, we have learned about Python tkinter messagebox. We have also covered these topics.

  • Python tkinter messagebox
  • python tkinter messagebox options
  • python tkinter messagebox size
  • python tkinter messagebox askquestion
  • python tkinter messagebox askyesno
  • python tkinter messagebox position
  • python tkinter messagebox documentation
  • Python tkinter yes no message box
  • Python tkinter yes no dialog box