当前位置:   article > 正文

Python Tkinter:messagebox_python messagebox.show

python messagebox.show

messagebox!显示一个消息窗口

目录

【1】参数部分

【2】函数

【1】_show

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

【3】ask...(askquestion等)

[3]关于使用


【1】参数部分

  1. __all__ = ["showinfo", "showwarning", "showerror",
  2. "askquestion", "askokcancel", "askyesno",
  3. "askyesnocancel", "askretrycancel"]
  4. #
  5. # constants
  6. # icons
  7. ERROR = "error"
  8. INFO = "info"
  9. QUESTION = "question"
  10. WARNING = "warning"
  11. # types
  12. ABORTRETRYIGNORE = "abortretryignore"
  13. OK = "ok"
  14. OKCANCEL = "okcancel"
  15. RETRYCANCEL = "retrycancel"
  16. YESNO = "yesno"
  17. YESNOCANCEL = "yesnocancel"
  18. # replies
  19. ABORT = "abort"
  20. RETRY = "retry"
  21. IGNORE = "ignore"
  22. OK = "ok"
  23. CANCEL = "cancel"
  24. YES = "yes"
  25. NO = "no"

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

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

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

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

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

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

包含了许多的样式:键。

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

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

ok:确定

okcancel:确定,取消

retrycancel:重试,取消

yesno:是,否

yesnocancel:是,否,重试

例:

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

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

-----------

终止:abort

重试:retry

取消:cancel

是:yes

否:no

确定:ok

忽略:ignore

-----------

【2】函数

【1】_show

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

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

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

或者

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

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

相当于

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

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

所以不用showquestion.

messagebox.py里的源码:

  1. def showinfo(title=None, message=None, **options):
  2. "Show an info message"
  3. return _show(title, message, INFO, OK, **options)
  4. def showwarning(title=None, message=None, **options):
  5. "Show a warning message"
  6. return _show(title, message, WARNING, OK, **options)
  7. def showerror(title=None, message=None, **options):
  8. "Show an error message"
  9. return _show(title, message, ERROR, OK, **options)

【3】ask...(askquestion等)

相当于

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

源码:

  1. def askokcancel(title=None, message=None, **options):
  2. "Ask if operation should proceed; return true if the answer is ok"
  3. s = _show(title, message, QUESTION, OKCANCEL, **options)
  4. return s == OK
  5. def askyesno(title=None, message=None, **options):
  6. "Ask a question; return true if the answer is yes"
  7. s = _show(title, message, QUESTION, YESNO, **options)
  8. return s == YES
  9. def askyesnocancel(title=None, message=None, **options):
  10. "Ask a question; return true if the answer is yes, None if cancelled."
  11. s = _show(title, message, QUESTION, YESNOCANCEL, **options)
  12. # s might be a Tcl index object, so convert it to a string
  13. s = str(s)
  14. if s == CANCEL:
  15. return None
  16. return s == YES
  17. def askretrycancel(title=None, message=None, **options):
  18. "Ask if operation should be retried; return true if the answer is yes"
  19. s = _show(title, message, WARNING, RETRYCANCEL, **options)
  20. return s == RETRY

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

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

[3]关于使用

直接调用就可以

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

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

  1. if __name__ == "__main__":
  2. print("info", showinfo("Spam", "Egg Information"))
  3. print("warning", showwarning("Spam", "Egg Warning"))
  4. print("error", showerror("Spam", "Egg Alert"))
  5. print("question", askquestion("Spam", "Question?"))
  6. print("proceed", askokcancel("Spam", "Proceed?"))
  7. print("yes/no", askyesno("Spam", "Got it?"))
  8. print("yes/no/cancel", askyesnocancel("Spam", "Want it?"))
  9. print("try again", askretrycancel("Spam", "Try again?"))

直接运行,然后输出。

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

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

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/124876
推荐阅读
相关标签
  

闽ICP备14008679号