Python Tkinter:messagebox

messagebox!显示一个消息窗口

目录

【1】参数部分

【2】函数

【1】_show

【2】show...(showwarning,showquestion等) 

【3】ask...(askquestion等)

[3]关于使用


【1】参数部分

__all__ = ["showinfo", "showwarning", "showerror",
           "askquestion", "askokcancel", "askyesno",
           "askyesnocancel", "askretrycancel"]

#
# constants

# icons
ERROR = "error"
INFO = "info"
QUESTION = "question"
WARNING = "warning"

# types
ABORTRETRYIGNORE = "abortretryignore"
OK = "ok"
OKCANCEL = "okcancel"
RETRYCANCEL = "retrycancel"
YESNO = "yesno"
YESNOCANCEL = "yesnocancel"

# replies
ABORT = "abort"
RETRY = "retry"
IGNORE = "ignore"
OK = "ok"
CANCEL = "cancel"
YES = "yes"
NO = "no"

包含了许多的样式:图标。

图标:error (错误)-[红色圆圈,里面有白色叉号]

,info(信息)-[蓝色圆圈,里面有白色i],

question(问题)-[蓝色圆圈,里面有白色?]和

warning(警告)-[黄色三角,里面有白色!].

--------------------------------------------------------------------------------

包含了许多的样式:键。

abortretryignore, ok, okcancel, retrycancel, yesno, 和 yesnocancel。

用法:abortretryignore :终止,重试,忽略

ok:确定

okcancel:确定,取消

retrycancel:重试,取消

yesno:是,否

yesnocancel:是,否,重试

例:

from tkinter import messagebox
a=messagebox._show("title","text","question","abortretryignore")
print(a)

当我们点击【终止】键时,打印出了:abort.

-----------

终止:abort

重试:retry

取消:cancel

是:yes

否:no

确定:ok

忽略:ignore

-----------

【2】函数

【1】_show

tkinter.messagebox._show( title(标题),message(文字),_icon(上述的图标,如question),_type(上述的键,如ok) )

例:要做一个标题为"1",正文为"1",图标为错误警告,样式为确定的窗口:

import tkinter.messagebox
a=tkinter.messagebox._show("1","1",messagebox.ERROR,messagebox.OK)

或者

import tkinter.messagebox
a=tkinter.messagebox._show("1","1","error","ok")

【2】show...(showwarning,showquestion等) 

相当于

tkinter.messagebox._show(title,message,"error","ok")#这是showerror
tkinter.messagebox._show(title,message,"warning","ok")#这是showwarning
tkinter.messagebox._show(title,message,"info","ok")#这是showinfo

重点!没有showquestion,因为question表示疑问。只有一个确定?好像不太行。 

所以不用showquestion.

messagebox.py里的源码:

def showinfo(title=None, message=None, **options):
    "Show an info message"
    return _show(title, message, INFO, OK, **options)


def showwarning(title=None, message=None, **options):
    "Show a warning message"
    return _show(title, message, WARNING, OK, **options)


def showerror(title=None, message=None, **options):
    "Show an error message"
    return _show(title, message, ERROR, OK, **options)

【3】ask...(askquestion等)

相当于

tkinter.messagebox._show(title,message,"question","yesno")#这是askquestion
tkinter.messagebox._show(title,message,"question","okcancel")#这是askokcancel
##以此类推

源码:

def askokcancel(title=None, message=None, **options):
    "Ask if operation should proceed; return true if the answer is ok"
    s = _show(title, message, QUESTION, OKCANCEL, **options)
    return s == OK


def askyesno(title=None, message=None, **options):
    "Ask a question; return true if the answer is yes"
    s = _show(title, message, QUESTION, YESNO, **options)
    return s == YES


def askyesnocancel(title=None, message=None, **options):
    "Ask a question; return true if the answer is yes, None if cancelled."
    s = _show(title, message, QUESTION, YESNOCANCEL, **options)
    # s might be a Tcl index object, so convert it to a string
    s = str(s)
    if s == CANCEL:
        return None
    return s == YES


def askretrycancel(title=None, message=None, **options):
    "Ask if operation should be retried; return true if the answer is yes"
    s = _show(title, message, WARNING, RETRYCANCEL, **options)
    return s == RETRY

 不过,它返回的是一个布尔值。

-*-自己看源码!-*-

[3]关于使用

直接调用就可以

from tkinter import messagebox
a=messagebox._show("title","text","question","abortretryignore")
print(a)

这是源码里的调用示例部分:

if __name__ == "__main__":

    print("info", showinfo("Spam", "Egg Information"))
    print("warning", showwarning("Spam", "Egg Warning"))
    print("error", showerror("Spam", "Egg Alert"))
    print("question", askquestion("Spam", "Question?"))
    print("proceed", askokcancel("Spam", "Proceed?"))
    print("yes/no", askyesno("Spam", "Got it?"))
    print("yes/no/cancel", askyesnocancel("Spam", "Want it?"))
    print("try again", askretrycancel("Spam", "Try again?"))

直接运行,然后输出。

效果的话可以自己copy一下,但注意要把函数改成messagebox.函数,例

showinfo改为messagebox.showinfo(import语句为:from tkinter import messagebox)

或改为tkinter.messagebox.showinfo(import语句为:import tkinter.messagebox)

--------------------------------------------完--------------------------------------------------------------------------------

你可能感兴趣的:(Python从负无穷到~,python,开发语言)