Tkinter Messagebox Tutorial
The tkinter module in Python has Messagebox module inside, which provides 8 dialogs, which can be applied in different occasions, as explained in this section.
(1)showinfo(title,message,options): display general prompt message.
from tkinter import *
from tkinter import messagebox
def myMsg(): # Executed when the Hello button is clicked
result = messagebox.showinfo("Customize the title of the message box","Welcome to apidemos.com")
print("messagebox return value:",result)
root = Tk()
root.title("apidemos.com")
root.geometry("300x160")
Button(root,text="Hello",command=myMsg).pack()
root.mainloop()
Output:
(2) showwarning(title,message,options): Show the warning message.
from tkinter import *
from tkinter import messagebox
def myMsg(): # Executed when the Hello button is clicked
result = messagebox.showwarning("Customize the title of messagebox","Welcome to apidemos.com")
print("messagebox return value:",result)
root = Tk()
root.title("apidemos.com")
root.geometry("300x160")
Button(root,text="Hello",command=myMsg).pack()
root.mainloop()
Output:
(3)showerror(title,message,options):显示错误消息。
from tkinter import *
from tkinter import messagebox
def myMsg(): # Executed when the Hello button is clicked
result = messagebox.showerror("Customize the title of messagebox","Welcome to apidemos.com")
print("messagebox return value:",result)
root = Tk()
root.title("apidemos.com")
root.geometry("300x160")
Button(root,text="Hello",command=myMsg).pack()
root.mainloop()
Output:
(4) askquestion(title,message,options): display the question message. If you click the "yes" button will send back "yes", if you click the "no" button will send back "no".
from tkinter import *
from tkinter import messagebox
def myMsg(): # Executed when the Hello button is clicked
result = messagebox.askquestion("Customize the title of messagebox","Welcome to apidemos.com")
print("messagebox return value:",result)
root = Tk()
root.title("apidemos.com")
root.geometry("300x160")
Button(root,text="Hello",command=myMsg).pack()
root.mainloop()
Output:
(5) askokcancel(title,message,options): Show OK or cancel message. If you click the "OK" button will pass back True, if you click the "Cancel" button will pass back False.
from tkinter import *
from tkinter import messagebox
def myMsg(): # Executed when the Hello button is clicked
result = messagebox.askokcancel("Customize the title of messagebox","Welcome to apidemos.com")
print("messagebox return value:",result)
root = Tk()
root.title("apidemos.com")
root.geometry("300x160")
Button(root,text="Hello",command=myMsg).pack()
root.mainloop()
Output:
(6) askyesno(title,message,options): display "yes or no" message. If you click the "Yes" button will be passed back True, if you click the "No" button will be passed back False.
from tkinter import *
from tkinter import messagebox
def myMsg(): # Executed when the Hello button is clicked
result = messagebox.askyesno("Customize the title of messagebox","Welcome to apidemos.com")
print("messagebox return value:",result)
root = Tk()
root.title("apidemos.com")
root.geometry("300x160")
Button(root,text="Hello",command=myMsg).pack()
root.mainloop()
Output:
(7) askyesnocancel(title,message,options): display "Yes or No or Cancel" message, if you click the "Yes" button will return True, if you click the "No" button will return False, if you click the "Cancel" button to return None.
from tkinter import *
from tkinter import messagebox
def myMsg(): # Executed when the Hello button is clicked
result = messagebox.askyesnocancel("Customize the title of messagebox","Welcome to apidemos.com")
print("messagebox return value:",result)
root = Tk()
root.title("apidemos.com")
root.geometry("300x160")
Button(root,text="Hello",command=myMsg).pack()
root.mainloop()
Output:
(8) askretrycancel(title,message,options): display "retry or cancel" message. If you click the "Retry" button will return True, if you click the "Cancel" button will return False.
from tkinter import *
from tkinter import messagebox
def myMsg(): # Executed when the Hello button is clicked
result = messagebox.askretrycancel("Customize the title of messagebox","Welcome to apidemos.com")
print("messagebox return value:",result)
root = Tk()
root.title("apidemos.com")
root.geometry("300x160")
Button(root,text="Hello",command=myMsg).pack()
root.mainloop()
Output:
The parameters within the above dialog methods are roughly the same, title is the name of the dialog, message is the text within the dialog, options is an optional parameter, and the possible values are the following three.
(1) default constant: the default button is OK, Yes, Retry in front, you can also change this setting.
(2) icon (constant): can set the icon displayed, there are INFO, ERROR, QUESTION, WARNING 4 kinds of icons can be set.
(3) parent(widget): point out that when the dialog is closed, the focus window will return to this parent window.
The last thing to note is that the above dialogs are placed under the message module in the tkinter module, so if you want to use these default dialogs, you need to add the following import statement at the beginning of the program.
from tkinter import messagebox
Tkinter Messagebox Tutorial Example#1
Basic application of dialog box design.
from tkinter import *
from tkinter import messagebox
def myMsg(): # Executed when the Hello button is clicked
result = messagebox.showinfo("Customize the title of messagebox","Welcome to apidemos.com")
print("messagebox return value:",result)
root = Tk()
root.title("apidemos.com")
root.geometry("300x160")
Button(root,text="Hello",command=myMsg).pack()
root.mainloop()
Output:
When using Messagebox, it is easy to establish a dialogue with the user. Although the content of the response when the user clicks a button has already been explained, the following line of the design program demonstrates the information returned when the user clicks a function button.
Tkinter Messagebox Tutorial Example#2
Design two buttons that bring up a dialog box when the button is clicked and list the response in a Python Shell window when the user responds.
from tkinter import *
from tkinter import messagebox
def myMsg1(): # Executed when the Installation failed button is clicked
ret = messagebox.askretrycancel("Test1","Installation failed, try again?")
print("Installation failed",ret)
def myMsg2(): # Executed when the Edit Complete button is clicked
ret = messagebox.askyesnocancel("Test2","Editing complete, yes or no or cancel?")
print("Edit Complete",ret)
root = Tk()
root.title("apidemos.com")
Button(root,text="Installation failed",command=myMsg1).pack()
Button(root,text="Edit Complete",command=myMsg2).pack()
root.mainloop()
Output:
With the value passed back by the user clicking the button, you can do further operations on the returned value.