messagebox消息框:
默认情况下,调用 messagebox 的工具函数时只要设置标题和提示区即可,提示区图标、按钮区的按钮都有默认设置;
messagebox函数9个:showinfo、showwarning、showerror、askquestion、askyesno、askyesnocancel、askokcancel、askretrycancel、messagebox._show(title、message、icon、type)
icon选项:“error”、“info”、“question”、“warning”
按钮类型type:“yesno”(是、否)、yesnocancel”(是、否、取消)、“ok”(确定)、“okcancel”(确定、取消)、“retrycancel”(重试、取消)、“abortretryignore”(取消、重试、忽略)
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
root = Tk()
root.title('消息框')
root.resizable(1,1)
#topF = Frame(root).pack(side=TOP,fill=X,expand =1,padx=5,pady=5)不能用这种,排序会不正常,已试验。
topLF = ttk.LabelFrame(root,text='默认图标和按钮类型')
topLF.pack(side=TOP,fill=X,expand =1,padx=5,pady=5)
def showinfo_clicked():
messagebox.showinfo("showinfo", "调用messagebox.showinfo函数")
def showwarning_clicked():
messagebox.showwarning("showwarning","调用messagebox.showwarning函数")
def showerror_clicked():
messagebox.showerror("showerror", "调用messagebox.showerror函数")
def askquestion_clicked():
messagebox.askquestion("askquestion", "调用messagebox.askquestion函数")
def askyesno_clicked():
print(messagebox.askyesno("askyesno", "调用messagebox.askyesno函数"))
def askyesnocancel_clicked():
print(messagebox.askyesnocancel("askyesnocancel", "调用messagebox.askyesnocancel函数"))
def askokcancel_clicked():
print(messagebox.askokcancel("askokcancel", "调用messagebox.askokcancel函数."))
def askretrycancel_clicked():
print(messagebox.askretrycancel("askretrycancel", "调用messagebox.askretrycancel函数"))
Button(topLF,text='显示信息框',command=showinfo_clicked).pack(side=LEFT,ipadx=5,ipady=5,padx=5)
Button(topLF,text='显示警告框',command=showwarning_clicked).pack(side=LEFT,ipadx=5,ipady=5,padx=5)
Button(topLF,text='显示错误框',command=showerror_clicked).pack(side=LEFT,ipadx=5,ipady=5,padx=5)
Button(topLF,text='问题框',command=askquestion_clicked).pack(side=LEFT,ipadx=5,ipady=5,padx=5)
Button(topLF,text='是否框',command=askyesno_clicked).pack(side=LEFT,ipadx=5,ipady=5,padx=5)
Button(topLF,text='是否取消框',command=askyesnocancel_clicked).pack(side=LEFT,ipadx=5,ipady=5,padx=5)
Button(topLF,text='确定取消框',command=askokcancel_clicked).pack(side=LEFT,ipadx=5,ipady=5,padx=5)
Button(topLF,text='重试取消框',command=askretrycancel_clicked).pack(side=LEFT,ipadx=5,ipady=5,padx=5)
bottomLF = ttk.LabelFrame(root,text='自定义图标和按钮类型')
bottomLF.pack(side=TOP, fill=X, expand=YES,padx=5, pady=5)
upF = Frame(bottomLF)
upF.pack(side=TOP, fill=X, expand=YES,padx=5, pady=5)
i = 0
iconVar = IntVar()
icons = [None, "info","warning", "error","question"]
for icon in icons:
RB = Radiobutton(upF, text = icon if icon is not None else '默认',value=i,variable=iconVar)
RB.pack(side=LEFT)
i += 1
iconVar.set(0)
downF = Frame(bottomLF)
downF.pack(side=TOP, fill=X, expand=YES,padx=5, pady=5)
def messagebox_show_clicked():
print(messagebox._show("messagebox._show", "调用messagebox._show函数,与上面有区别",icon=icons[iconVar.get()],type="abortretryignore"))
btn = Button(downF,text='终止重试忽略框',command=messagebox_show_clicked)
btn.pack(side=LEFT,ipadx=5,ipady=5,padx=5)
root.mainloop()
来源:CSDN
作者:flyingc
链接:https://blog.csdn.net/u011878611/article/details/103854980