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

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

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

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

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