消息窗口组件Messagebox用于弹出提示框向用户进行警告,或让用户下一步如何操作。消息框包括很多类型,常用的有
info、warning、error、yesno、okcancel等包含不同的图标、按钮以及弹出提示音。
# 演示各消息框的程序及运行效果
import tkinter as tk
from tkinter import messagebox as ms
def b1_clicked():
ms.showinfo("Info","Showinfo test.")
def b2_clicked():
ms.showwarning("Warning", "Showwarning test.")
def b3_clicked():
ms.showerror("Error", "Showerror test.")
def b4_clicked():
ms.askquestion("Question", "Askquestion test.")
def b5_clicked():
ms.askokcancel("OkCancel", "Askokcancel test.")
def b6_clicked():
ms.askyesno("YesNo", "Askyesno test.")
def b7_clicked():
ms.askretrycancel("Retry", "Askretrycancel test.")
root = tk.Tk()
root.title("Msgbox Text")
b1 = tk.Button(root,text="showinfo",command=b1_clicked)
b1.pack(fill=tk.X)
b2 = tk.Button(root,text="showwarning",command=b2_clicked)
b2.pack(fill=tk.X)
b3 = tk.Button(root,text="showerror",command=b3_clicked)
b3.pack(fill=tk.X)
b4 = tk.Button(root,text="askquestion",command=b4_clicked)
b4.pack(fill=tk.X)
b5 = tk.Button(root,text="askohcanel",command=b5_clicked)
b5.pack(fill=tk.X)
b6 = tk.Button(root,text="askyseno",command=b6_clicked)
b6.pack(fill=tk.X)
b7 = tk.Button(root,text="askretrycancel",command=b7_clicked)
b7.pack(fill=tk.X)
root.mainloop()
结果: