Today we will cover message box widget in Python
The messagebox widget
The messagebox widget is a widget which is used to represent pop-up message in a Tkinter program.
The program below shows an example of a simple messagebox.
from tkinter import messagebox
from tkinter import *
master = Tk()
master.geometry("100x100")
def showmessage():
messagebox.showinfo("Say hello","Hello programmers")
button=Button(master,text="Show message",command=showmessage)
button.pack()
master.mainloop()
Syntax- messagebox.showinfo(title,message)
Similarly The message box has different flavors of boxes
-
showinfo
- show message on popup -
showwarning
- show warning (with disclaimer sign) -
showerror
- Show error (with red cross) -
askquestiion
- Asks the user yes or no -
askyesno
- same likeaskquestion
-
askokcancel
Asks the user ok or cancel -
askretrycancel
asks the user retry or cancel
Warning
from tkinter import messagebox
from tkinter import *
master = Tk()
master.geometry("100x100")
def showmessage():
messagebox.showwarning("Wait!","Something is missing")
button=Button(master,text="Show message",command=showmessage)
button.pack()
master.mainloop()
Showing error
from tkinter import messagebox
from tkinter import *
master = Tk()
master.geometry("100x100")
def showmessage():
messagebox.showerror("Error!","Something is wrong")
button=Button(master,text="Show message",command=showmessage)
button.pack()
master.mainloop()
Asking question
Asking question is quite similar to askyesno
. Both give the same output
from tkinter import messagebox
from tkinter import *
master = Tk()
master.geometry("100x100")
def showmessage():
messagebox.askquestion("stop!","are you sure?")
button=Button(master,text="Show message",command=showmessage)
button.pack()
master.mainloop()
from tkinter import messagebox
from tkinter import *
master = Tk()
master.geometry("100x100")
def showmessage():
messagebox.askyesno("stop!","are you sure?")
button=Button(master,text="Show message",command=showmessage)
button.pack()
master.mainloop()
Extracting values from the answer
We can get the values of the user entered answer in form of true or false from the user as shown in the program below
from tkinter import messagebox
from tkinter import *
master = Tk()
master.geometry("100x100")
def showmessage():
a=messagebox.askyesno("stop!","are you sure?")
#returns true or false
button.config(text=a)
button=Button(master,text="Show message",command=showmessage)
button.pack()
master.mainloop()
On clicking yes,
Ask ok, cancel
from tkinter import messagebox
from tkinter import *
master = Tk()
master.geometry("100x100")
def showmessage():
a=messagebox.askokcancel("stop!","Do it?")
#returns true or false
button.config(text=a)
button=Button(master,text="Show message",command=showmessage)
button.pack()
master.mainloop()
We can get the values from the box in the same manner as askyesno
Ask Retry cancel
from tkinter import messagebox
from tkinter import *
master = Tk()
master.geometry("100x100")
def showmessage():
a=messagebox.askretrycancel("stop!","Action failed?")
#returns true or false
button.config(text=a)
button=Button(master,text="Show message",command=showmessage)
button.pack()
master.mainloop()
Top comments (0)