Python tkinter messagebox


Youtube Live session on Tkinter

Tkinter Message box to show info & to take confirmation choice as input by showing options to user

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.
MessageBox of the tkinter
Let us learn different type of message boxes we can use and basic syntax
tkMessageBox.typeofmessage(title, message [, options]).
Show Message only :
showerror, showinfo,showwarning
Show Message and get user selection :
askokcancel,askyesno,askquestion,askretrycancel,askyesnocancel

showerror

Message box on showerror
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

Message box on showinfo
#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()

showwarning

Message box on showwarning
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.

askokcancel

Message box on askokcancel
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()

askyesno

Message box on askyesno
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()

askretrycancel

Message box on askretrycancel
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()

askyesnocancel

Message box on askyesnocancel
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()

Default button ( Optional )

Message box button option
We can’t change the text in button but we can change the default selection of buttons.
default value can be 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()

icon ( Optional )

Message box icon option
We can change the default icon comes with the different types of message boxes.
icon = 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()

Displaying response in window

On click of a Button the message will be displayed. The user response will be collected and displayed through one Label.
Displaying response of Message box
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 

Customizing message

Customized Message in Message box
We can change the message part here and show message by using variables.
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))
  1. Exercise on MessageBox
  2. Enter a series of radio button and then show matching messagebox based on the selection and then take input from user and show the value of user selected choice
  3. We can set the optional parameters icon to four different values ( info, question, warning, error ) . Create four radio buttons and on click of each button will display one showinfo() messagebox with selected type of icon. Inside the message box you can set the matching title and body message while displaying the different icons.
  4. In a message box we can set default selection of button by setting default value. Create three radio buttons which on select will set the value of default selection of buttons. The message box should display matching title and body message while setting the default button
Solution
simpledialog to take user inputs

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer