tkinter 提供不同类型的对话框,这些对话框的功能存放在 tkinter 的不同子模块中,主要包括 messagebox 模块、filedialog 模块和 colorchooser 模块。我们会对以上三个模块分别进行单独的讲解,在本节主要介绍 messagebox 模块。
messagebox 模块提供以下方法打开供用户选择项目的对话框:
(1)askokcancel(title=None, message=None) #
打开一个“确定/取消”的对话框。例如:
import tkinter.messagebox tkinter.messagebox.askokcancel("提示", " 你确定要关闭窗口吗? ")
结果如图所示:

(2)askquestion(title=None, message=None) #
打开一个“是/否”的对话框。例如:
import tkinter.messagebox tkinter.messagebox.askquestion("提示", " 你确定要关闭窗口吗? ")
结果如图所示:

(3)askretrycancel(title=None, message=None) #
打开一个“重试/取消”的对话框。例如:
import tkinter.messagebox tkinter.messagebox.askretrycancel("提示", "你确定要关闭窗口吗? ")
结果如图所示:

(4)askyesno(title=None, message=None) #
import tkinter.messagebox tkinter.messagebox.askyesno("提示", "你确定要关闭窗口吗? ")
结果如图所示:

(5)showerror(title=None, message=None) #
打开一个错误提示对话框。例如:
import tkinter.messagebox tkinter.messagebox.showerror("提示", "你确定要关闭窗口吗? ")
结果如图所示:

(6)showinfo(title=None, message=None) #
打开一个信息提示对话框。例如:
import tkinter.messagebox tkinter.messagebox.showinfo("提示", "你确定要关闭窗口吗? ")
结果如图所示:

(7)showwarning(title=None, message=None) #
打开一个警告提示对话框。例如:
import tkinter.messagebox tkinter.messagebox.showwarning("提示", "你确定要关闭窗口吗? ")
结果如图所示:
