tkMessageBox模塊用於顯示在您的應用程序的消息框。此模塊提供了一個功能,您可以用它來顯示適當的消息.
這些功能有些是showinfo,showwarning,showerror,askquestion,askokcancel,askyesno,askretryignore.
方法:
這里是一個簡單的語法來創建這個widget:
tkMessageBox.FunctionName(title, message [, options])
參數:
-
FunctionName: 這是相應的消息框函數的名稱.
-
title: 這是在一個消息框,標題欄顯示的文本.
-
message: 這是要顯示的文字作為消息.
-
options: 選項有替代的選擇,你可以用它來定制一個標准的消息框。一些可以使用的選項是默認的。默認選項是用來指定默認的按鈕,如中止,重試,或忽略在消息框中。父選項是用來指定要顯示的消息框上的頂層窗口.
If the standard message boxes are not appropriate, you can pick the closest alternative (askquestion, in most cases), and use options to change it to exactly suit your needs. You can use the following options (note that messageand title are usually given as arguments, not as options).
default constant
Which button to make default: ABORT, RETRY, IGNORE, OK, CANCEL,YES, or NO (the constants are defined in the tkMessageBox module).
icon (constant)
Which icon to display: ERROR, INFO, QUESTION, or WARNING
message (string)
The message to display (the second argument to the convenience functions). May contain newlines.
parent (widget)
Which window to place the message box on top of. When the message box is closed, the focus is returned to the parent window.
title (string)
Message box title (the first argument to the convenience functions).
type (constant)
Message box type; that is, which buttons to display:ABORTRETRYIGNORE, OK, OKCANCEL, RETRYCANCEL, YESNO, orYESNOCANCEL.
-
showinfo()
-
showwarning()
-
showerror ()
-
askquestion()
-
askokcancel()
-
askyesno ()
-
askretrycancel ()
例子:
自行嘗試下面的例子:
import Tkinter import tkMessageBox top = Tkinter.Tk() def hello(): tkMessageBox.showinfo("Say Hello", "Hello World") B1 = Tkinter.Button(top, text = "Say Hello", command = hello) B1.pack() top.mainloop()
這將產生以下結果:
不顯示window root窗口
import Tkinter import tkMessageBox top = Tkinter.Tk() top.withdraw() tkMessageBox.askyesno("Say Hello", "Hello World")