TkInter 訊息框
Tkinter tkMessageBox 有各種方法來顯示訊息框。
Tkinter for Python 2.7 和 Python 3 之間略有不同。要查詢 Python 版本,請使用以下命令之一:
python --version
python3 --version
Tkinter 訊息框
![TkMessage 訊息框](/img/Tutorial/Tkinter/Tkinter Message Box.png)
要顯示簡約的 Tkinter 訊息框,請使用 showinfo()
函式,其中引數是視窗標題和文字。
showinfo()
函式位於不同的模組中,具體取決於 Python 版本。
Python 3.x
from tkinter import messagebox
messagebox.showinfo("Title", "a Tk MessageBox")
Python 2.7
import Tkinter
import tkMessageBox
tkMessageBox.showinfo("Title", "a Tk MessageBox")
Tkinter showerror,showwarning 和 showinfo
![TkMessage 訊息框 showerror](/img/Tutorial/Tkinter/Tkinter MessageBox showerror.png) ![TkMessage 訊息框 showwarning](/img/Tutorial/Tkinter/Tkinter MessageBox showwarning.png) ![TkMessage 訊息框 showinfo](/img/Tutorial/Tkinter/Tkinter MessageBox showinfo.png) Tkinter 包含其他幾個訊息框:
showerror()
showwarning()
showinfo()
Python 3.x
import tkinter
from tkinter import messagebox
# hide main window
root = tkinter.Tk()
root.withdraw()
# message box display
messagebox.showerror("Error", "Error message")
messagebox.showwarning("Warning","Warning message")
messagebox.showinfo("Information","Informative message")
Python 2.7
import Tkinter
import tkMessageBox
# An error box
tkMessageBox.showerror("Error","No disk space left on device")
# A warning box
tkMessageBox.showwarning("Warning","Could not start service")
# An information box
tkMessageBox.showinfo("Information","Created in Python.")