import tkinter as tk
my_w = tk.Tk()
from tkinter import messagebox as msg
my_w.geometry("500x500") # Size of the window
msg.showinfo("Title Here","Your Message here")
#messagebox.showerror("error","Error")
my_w.mainloop() # Keep the window open
Above window shows information only, we can collect user selection also. tkMessageBox.typeofmessage(title, message [, options]).
Show Message only : import tkinter as tk
from tkinter import messagebox as msg
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
msg.showerror("Title Here ","Your Message here ")
my_w.mainloop()
#showinfo
import tkinter as tk
from tkinter import messagebox as msg
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
msg.showinfo("Title Here ","Your info message here ")
my_w.mainloop()
import tkinter as tk
from tkinter import messagebox as msg
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
msg.showwarning("Title Here ","Your warning message here ")
my_w.mainloop()
We will ask user to select choice and we will capture the selected option through a variable.
import tkinter as tk
from tkinter import messagebox as msg
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
my_var=msg.askokcancel("Title Here ","Your Choice ")
print(my_var) # Output True or False
my_w.mainloop()
import tkinter as tk
from tkinter import messagebox as msg
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
my_var=msg.askyesno("Title Here ","Your Choice ")
print(my_var) # Output True or False
my_w.mainloop()
import tkinter as tk
from tkinter import messagebox as msg
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
my_var=msg.askretrycancel("Title Here ","Your idea")
print(my_var) # Output True or False
my_w.mainloop()
import tkinter as tk
from tkinter import messagebox as msg
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
my_var=msg.askyesnocancel("Title Here ","Your choice")
print(my_var) # Output True or False or None
my_w.mainloop()
yes
, no
or cancel
import tkinter as tk
from tkinter import messagebox as msg
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
my_var=msg.askyesnocancel("Title Here ","Your choice",default='cancel')
# default = yes or no or cancel
print(my_var)
my_w.mainloop()
warning
or info
or question
or error
import tkinter as tk
from tkinter import messagebox as msg
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
my_var=msg.askyesnocancel("Title Here ","Your choice",icon='warning')
# icon = warning or error or information or question
print(my_var)
my_w.mainloop()
import tkinter as tk
from tkinter import messagebox as msg
my_w = tk.Tk()
my_w.geometry("300x200") # Size of the window
#my_var=tk.StringVar(my_w)
def show_msg(): # on click of the button b1
my_var=msg.askyesnocancel('Title Here', 'Message here')
print(my_var) # print at console
l1.config(text=str(my_var)) # display at Label
b1=tk.Button(my_w,text='Show Message',command=show_msg)
b1.grid(row=1,column=1,padx=30,pady=30)
my_font=('times',18,'bold')
l1=tk.Label(my_w,text='Output here',font=my_font,bg='yellow')
l1.grid(row=2,column=1)
my_w.mainloop()
The output will show as 0 or 1, to display as string saying True, False , None , yes, no We have to convert the same to string by using str()
l1.config(text=str(my_var)) # display at Label
def show_msg():
str1='Alex'
str2=44
m1="This is OK Mr {0} \n You R deleting id: {1}".format(str1,str2)
my_var=msg.askyesnocancel('Title Here', m1)
print(my_var)
l1.config(text=str(my_var))
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.