Home C C++ Java Python Perl PHP SQL JavaScript Linux Selenium QT Online Test

Home » Python » Tkinter » MessageBox

Python Tkinter MessageBox Example

Python Tkinter support 6 types of messagebox/popup. Here is a list of supported messagebox types-

  • Ok Cancel messagebox
  • Info messagebox
  • Error messagebox
  • Warning messagebox
  • Ask Question messagebox
  • Ask Yes / No messagebox

from tkinter import *
from tkinter import messagebox
root = Tk()
root.title("Message Box in Python")
#root.iconbitmap('c:/icon.png')

def showPopup(type):
    response = ''

    if(type == 0):
        messagebox.showinfo("Hey!! Python programmer", "I am info messagebox")
    elif(type == 1):
        messagebox.showwarning("Hey!! Python programmer", "I am warning messagebox")
    elif(type == 2):
        messagebox.showerror("Hey! Python programmer", "I am error messagebox")
    elif(type == 3):
        messagebox.askquestion("Hey! Python programmer", "I am Ask Question messagebox")
    elif(type == 4):
        response = messagebox.askokcancel("Hey! Python programmer", "I am Ask Ok Cancel messagebox")
    elif(type == 5):
        response = messagebox.askyesno("Hey! Python programmer", "I am Ask Yes No messagebox")

    lbl = Label(root, text=response)
    lbl.grid(row=6, column = 0)

btn1 = Button(root, text="Info messagebox ", command=lambda:showPopup(0))
btn2 = Button(root, text="Warning messagebox", command=lambda:showPopup(1))
btn3 = Button(root, text="Error messagebox", command=lambda:showPopup(2))
btn4 = Button(root, text="Ask Question messagebox", command=lambda:showPopup(3))
btn5 = Button(root, text="Ask Ok Cancel messagebox", command=lambda:showPopup(4))
btn6 = Button(root, text="Yes No messagebox", command=lambda:showPopup(5))

btn1.grid(row=0, column=0)
btn2.grid(row=1, column=0)
btn3.grid(row=2, column=0)
btn4.grid(row=3, column=0)
btn5.grid(row=4, column=0)
btn6.grid(row=5, column=0)

root.mainloop()

Ok/Cancel type MessageBox

how to display messagebox/popup in Python Tkinter

Info type MessageBox

how to display messagebox/popup in Python Tkinter

Error MessageBox

how to display messagebox/popup in Python Tkinter

Warning MessageBox

how to display messagebox/popup in Python Tkinter

Ask Question using MessageBox

how to display messagebox/popup in Python Tkinter

Ask Yes/No using MessageBox

how to display messagebox/popup in Python Tkinter