Python tkinter messagebox solution

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.
import tkinter as tk
from tkinter import messagebox as msg
my_w = tk.Tk()
my_w.geometry("500x300")  # Size of the window 
my_val=''
def my_upd():
   # print(r1_v.get())
    if(r1_v.get()==1):
        my_val=msg.askokcancel("askokcancel","Select One Ok Cancel")
    elif(r1_v.get()==2):
        my_val=msg.askyesno("askyesno","Yes or No ")
    elif(r1_v.get()==3):
        my_val=msg.askretrycancel("asktrycancel","Select Try or Cancel")
    elif(r1_v.get()==4):
        my_val=msg.askyesnocancel("askyesnocancel","Select yes no cancel")    
    my_str.set(" : " + str(my_val))
    #print(my_val)

r1_v = tk.IntVar() # We used integer variable here 

r1 = tk.Radiobutton(my_w, text='askokcancel', variable=r1_v, value=1,command=my_upd)
r1.grid(row=1,column=1) 

r2 = tk.Radiobutton(my_w, text='askyesno', variable=r1_v, value=2,command=my_upd)
r2.grid(row=1,column=2)

r3 = tk.Radiobutton(my_w, text='askretrycancel', variable=r1_v, value=3,command=my_upd)
r3.grid(row=1,column=3)

r4 = tk.Radiobutton(my_w, text='askyesnocancel', variable=r1_v, value=4,command=my_upd)
r4.grid(row=1,column=4)

my_str = tk.StringVar()
l1 = tk.Label(my_w,  textvariable=my_str, width=10 )
l1.grid(row=2,column=2)
my_str.set('Details here')

my_w.mainloop()
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.
import tkinter as tk
my_w = tk.Tk()
from tkinter import messagebox as msg
my_w.geometry("500x300")  # Size of the window 
my_val=''
def my_upd():
   # print(r1_v.get())
    if(r1_v.get()==1):
        my_title="Value = 1"
        my_msg="This is info Icon"
        my_icon="info"
    elif(r1_v.get()==2):
        my_title="Value = 2"
        my_msg="This is Question Icon"
        my_icon="question"
    elif(r1_v.get()==3):
        my_title="Value = 3"
        my_msg="This is Warning  Icon"
        my_icon="warning"
    elif(r1_v.get()==4):
        my_title="Value = 4"
        my_msg="This is Error Icon"
        my_icon="error"
    my_val=msg.showinfo(my_title,my_msg,icon=my_icon)
    

r1_v = tk.IntVar() # We used integer variable here 

r1 = tk.Radiobutton(my_w, text='info', variable=r1_v, value=1,command=my_upd)
r1.grid(row=1,column=1) 

r2 = tk.Radiobutton(my_w, text='question', variable=r1_v, value=2,command=my_upd)
r2.grid(row=1,column=2)

r3 = tk.Radiobutton(my_w, text='warning', variable=r1_v, value=3,command=my_upd)
r3.grid(row=1,column=3)

r4 = tk.Radiobutton(my_w, text='error', variable=r1_v, value=4,command=my_upd)
r4.grid(row=1,column=4)

my_w.mainloop()
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
#selection of default selected button in a askyesnocancel 
import tkinter as tk
from tkinter import messagebox as msg
my_w = tk.Tk()
my_w.geometry("500x300")  # Size of the window 
my_val=''
def my_upd():
   # print(r1_v.get())
    if(r1_v.get()==1):
        my_title="Value = 1"
        my_msg="Default Selection is Yes"
        my_button="yes"
    elif(r1_v.get()==2):
        my_title="Value = 2"
        my_msg="Default Selection is No"
        my_button="no"
    elif(r1_v.get()==3):
        my_title="Value = 3"
        my_msg="Default Selection is Cancel"
        my_button="cancel"
    
    my_val=msg.askyesnocancel(my_title,my_msg,default=my_button)
    

r1_v = tk.IntVar() # We used integer variable here 

r1 = tk.Radiobutton(my_w, text='Yes', variable=r1_v, value=1,command=my_upd)
r1.grid(row=1,column=1) 

r2 = tk.Radiobutton(my_w, text='No', variable=r1_v, value=2,command=my_upd)
r2.grid(row=1,column=2)

r3 = tk.Radiobutton(my_w, text='Cancel', variable=r1_v, value=3,command=my_upd)
r3.grid(row=1,column=3)

my_w.mainloop()
Messagebox Tutorial & Exercise


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