Python tkinter messagebox

Displaying a messagebox with three elements ( PHP, Python,MySQL).
Basics of Python Tkinter
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()
  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



plus2net.com



Post your comments , suggestion , error , requirements etc here




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