本文链接:https://blog.csdn.net/weixin_45417938/article/details/105728333

在Tkinter包下的message模块下提供了大量的工具函数来生成各种下消息框
messagebox的工具函数大致分为图标区、提示区和按钮区
在这里插入图片描述

messagebox中工具函数如下:
1.消息提示框
tkinter.messagebox.showinfo(title,message,icon= None,type= None):
返回值为点击的按键的值,包括“yes”(是)、“no”(否)、“retry”(重试)、“ok”(确定)、“cancel”(取消)、“ignore”(忽略)、”abort“(中止)。

2.消息警告框
tkinter.messagebox.showwarning(title,message,icon= None,type= None):
返回值为点击的按键的值,包括“yes”(是)、“no”(否)、“retry”(重试)、“ok”(确定)、“cancel”(取消)、“ignore”(忽略)、”abort“(中止)。

3.消息错误框
tkinter.messagebox.showerror(title,message,icon= None,type= None):
返回值为点击的按键的值,包括“yes”(是)、“no”(否)、“retry”(重试)、“ok”(确定)、“cancel”(取消)、“ignore”(忽略)、”abort“(中止)。

4.对话框
tkinter.messagebox.askquestion(title,message,icon= None,type= None):
返回值为点击的按键的值,包括“yes”(是)、“no”(否)、“retry”(重试)、“ok”(确定)、“cancel”(取消)、“ignore”(忽略)、”abort“(中止)。

tkinter.messagebox.askokcancel(title,message,icon= None,type= None):
返回值为True或False,当单击的按钮值为“ok”(确定)时返回True,否则都为False

tkinter.messagebox.askyesno(title,message,icon= None,type= None):
返回值为True或False,当单击的按钮键值为“yes”(是)时返回True,否则都返回False

tkinter.messagebox.askyesnocancel(title,message,icon= None,type= None):
返回值为True、False、None,当单击的按键值为“yes”(是)时返回True、当单击的按键值为“cancel”(取消)时返回None,否则都返回False

tkinter.messagebox.askretrycancel(title,message,icon= None,type= None):
返回值为True或False,当单击的按钮值为“retry”(重试)时返回True,否则都为False

默认情况下使用者在调用messagebox时只要设置提示区字符串即可。但如果有需要,可以通过如下两个选项来设置图标和按键
icon:定制的图标区图标选项,该选项支持“error”、“info”、“question”、“warning”(默认为“info”图标)
在这里插入图片描述在这里插入图片描述在这里插入图片描述
在这里插入图片描述
type:定制按钮的选项。该选项支持“abortretryignore”(中止、重试、忽略)、“ok”(确定)、“okcancel”(确定、取消)、“retrycancel”(重试、取消)、“yesno”(是、否)、“yesnocancel”(是、否、取消)(默认为“ok”按键)
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述
在这里插入图片描述在这里插入图片描述
title:messagebox消息框的标题
在这里插入图片描述
message:提示区字符串
在这里插入图片描述

下面代码通过两组单选钮让用户动态选择不同的icon和type选项的效果:

from tkinter import *
# 导入ttk
from tkinter import ttk
# 导入messagebox
from tkinter import messagebox


class App:
    def __init__(self, master):
        self.master = master
        self.initWidgets()

    def initWidgets(self):
        # 创建第一个Labelframe,用于选择图标类型
        topF = ttk.Frame(self.master)
        topF.pack(fill=BOTH)
        lf1 = ttk.Labelframe(topF, text='请选择图标类型')
        lf1.pack(side=LEFT, fill=BOTH, expand=1, padx=10, pady=5)
        i = 0
        self.iconVar = IntVar()
        self.icons = [None, 'error', 'info', 'question', 'warning']
        # 使用循环创建多个RadioButton,并放入Labelframe中
        for icon in self.icons:
            ttk.Radiobutton(lf1, text=icon if icon is not None else '默认', value=i,
                            variable=self.iconVar).pack(side=TOP, anchor=W)
            i += 1
        self.iconVar.set(0)
        #创建第二个labelframe,用于选择按钮类型
        lf2 = ttk.Labelframe(topF, text='选择按钮类型')
        lf2.pack(side=LEFT, fill=BOTH, expand=1, padx=10, pady=5)
        i = 0
        self.typeVar = IntVar()
        self.types = [None, 'abortretryignore', 'ok', 'okcancel', 'retrycancel', 'yesno', 'yesnocancel']
        # 利用循环创建多个Radiobutton,并放入Labelframe中
        for tp in self.types:
            ttk.Radiobutton(lf2, text=tp if tp is not None else '默认', value=i, variable=self.typeVar).pack(side=TOP,
                                                                                                           anchor=W)
            i += 1
        self.typeVar.set(0)
        #创建Frame,用于包含多个按钮来生成不同的消息框
        bottomF = ttk.Frame(self.master)
        bottomF.pack(fill=BOTH)
        # 创建8个按钮,并为之绑定事件处理方法
        ttk.Button(bottomF, text='showinfo', command=self.showinfo_clicked).pack(side=LEFT, fill=X, ipadx=5, ipady=5,
                                                                                 padx=5, pady=5)
        ttk.Button(bottomF, text='showwarning', command=self.showwarning_clicked).pack(side=LEFT, fill=X, ipadx=5,
                                                                                       ipady=5, padx=5, pady=5)
        ttk.Button(bottomF, text='showerror', command=self.showerror_clicked).pack(side=LEFT, fill=X, ipadx=5, ipady=5,
                                                                                   padx=5, pady=5)
        ttk.Button(bottomF, text='askquestion', command=self.askquestion_clicked).pack(side=LEFT, fill=X, ipadx=5,
                                                                                       ipady=5, padx=5, pady=5)
        ttk.Button(bottomF, text='askokcancel', command=self.askokcancel_clicked).pack(side=LEFT, fill=X, ipadx=5,
                                                                                       ipady=5, padx=5, pady=5)
        ttk.Button(bottomF, text='askyesno', command=self.askyesno_clicked).pack(side=LEFT, fill=X, ipadx=5, ipady=5,
                                                                                 padx=5, pady=5)
        ttk.Button(bottomF, text='askyesnocancel', command=self.askyesnocancel_clicked).pack(side=LEFT, fill=X, ipadx=5,
                                                                                             ipady=5, padx=5, pady=5)
        ttk.Button(bottomF, text='askretrycancel', command=self.askretrycancel_clicked).pack(side=LEFT, fill=X, ipadx=5,
                                                                                             ipady=5, padx=5, pady=5)

    def showinfo_clicked(self):
        print(messagebox.showinfo('Info', 'showinfo测试', icon=self.icons[self.iconVar.get()],
                                  type=self.types[self.typeVar.get()]))

    def showwarning_clicked(self):
        print(messagebox.showwarning('Warning', 'showwarning测试', icon=self.icons[self.iconVar.get()],
                                     type=self.types[self.typeVar.get()]))

    def showerror_clicked(self):
        print(messagebox.showerror('Error', 'showerror测试', icon=self.icons[self.iconVar.get()],
                                   type=self.types[self.typeVar.get()]))

    def askquestion_clicked(self):
        print(messagebox.askquestion('Question', 'askquestion测试', icon=self.icons[self.iconVar.get()],
                                     type=self.types[self.typeVar.get()]))

    def askokcancel_clicked(self):
        print(messagebox.askokcancel('Okcancel', 'askokcancel测试', icon=self.icons[self.iconVar.get()],
                                     type=self.types[self.typeVar.get()]))

    def askyesno_clicked(self):
        print(messagebox.askyesno('Yesno', 'askyesno测试', icon=self.icons[self.iconVar.get()],
                                  type=self.types[self.typeVar.get()]))

    def askyesnocancel_clicked(self):
        print(messagebox.askyesnocancel('Yesnocancel', 'askyesnocancel', icon=self.icons[self.iconVar.get()],
                                        type=self.types[self.typeVar.get()]))

    def askretrycancel_clicked(self):
        print(messagebox.askretrycancel('Retrycancel', 'askretrycancel', icon=self.icons[self.iconVar.get()],
                                        type=self.types[self.typeVar.get()]))


root = Tk()
root.title('message测试')
App(root)
root.mainloop()
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697

运行程序:在这里插入图片描述

最后修改:2020 年 10 月 26 日
如果觉得我的文章对你有用,请随意赞赏