一目了然看代码:
import tkinter.messagebox from tkinter import * tkinter.messagebox.askokcancel("FishC Demo",'确定or取消?') tkinter.messagebox.askquestion("FishC Demo","你确定吗?") tkinter.messagebox.askretrycancel("FishC Demo","启动失败,重试?") tkinter.messagebox.askyesno("FishC Demo","是否继续?") tkinter.messagebox.showerror("FishC Demo","出错啦!") tkinter.messagebox.showinfo("FishC Demo","2019元旦快乐") tkinter.messagebox.showwarning("FishC Demo","请注意!") mainloop()
上面这段代码分别使用了messagebox中的对话框函数,下面总结一下上述函数:
1.参数
所有这些函数都有相同的参数:
(1)title:设置标题栏的文本
(2)message:设置对话框的主要文本内容,可以用’\n’来实现换行
(3)options:可以设置选项和含义
options参数可以设置的选项和含义
选项 | 含 义 |
default | 1.设置默认的按钮(也就是按下回车响应的那个按钮) 2.默认是第一个按钮(像“确定”“是”或“重试”) 3.可以设置的值根据对话框函数的不同可以选择: CANCEL、IGNORE、OK、NO、RETRY或Y |
icon | 1.指定对话框显示的图标 2.可以指定的值有: ERROR、INFO、QUESTION或WARNING 3.注意:不能指定自己的图标 |
parent | 1.如果不指定该选项,那么对话框默认显示在根窗口上 2. 如果想要将对话框显示在子窗口w上,那么可以设置parent= w |
2.返回值
askokcancel()、askretrycancel()、askyesno()返回布尔类型的值
图例说明:
- 提示消息框:【返回”ok”】

tkinter.messagebox.showinfo(消息框标题,提示内容)
- 消息警告框【返回”ok”】:

tkinter.messagebox.showwarning(消息框标题,警告内容)
- 错误消息框【返回”ok”】:

tkinter.messagebox.showerror(消息框标题,错误提示内容)
对话框:
- 询问确认对话框[返回”yes”,”no”]

tkinter.messagebox.askquestion(消息框标题,提示内容)
- 确认/取消对话框[返回True False]:

tkinter.messagebox.askokcancel(消息框标题,提示内容)
- 是/否对话框【返回True False】:

tkinter.messagebox.askyesno(消息框标题,提示内容)
- 重试/取消对话框:【返回值:True False】

tkinter.messagebox.askretrycancel(标题,提示内容)
- 是\否\取消对话框: 【返回值:是:True 否:False 取消:None】:
tkinter.messagebox.askyesnocancel(标题,提示内容)
from tkinter import * import tkinter.messagebox def info_warn_err(): a=tkinter.messagebox.showinfo("我的标题","我的提示1") print(a) a=tkinter.messagebox.showwarning("我的标题","我的提示2") print(a) a=tkinter.messagebox.showerror("我的标题", "我的提示3") print(a) def func2(): a=tkinter.messagebox.askyesno("我的标题","我的提示1") print(a) a=tkinter.messagebox.askokcancel("我的标题","我的提示2") print(a) a=tkinter.messagebox.askquestion("www.tianqiweiqi.com","我的提示3") print(a) a=tkinter.messagebox.askretrycancel("我的标题","我的提示4") print(a) a=tkinter.messagebox.askyesnocancel("我的标题","我的提示5") print(a) #这里用作演示如何使用对话框 if tkinter.messagebox.askyesno("我的标题", "确认关闭窗口吗!"): root.destroy() root=Tk() btn=Button(root,text="信息、警告、错误消息框",command=info_warn_err) btn1=Button(root,text="对话框",command=func2) btn.pack() btn1.pack() root.mainloop()