tkinter學習筆記之messagebox
messagebox是普通windows程式設計中經常用到的,目的是顯示個資訊框
如圖:
在python2.7
他的呼叫方式是:
from tkMessageBox import *
showinfo(message=“hello”)
python3.x
改為
from tkinter.messagebox import *
showinfo(message=“hello”)
除了showinfo方法外,還有
tkinter.messagebox中有如下函式:
askokcancel(title=None, message=None, **options)
Ask if operation should proceed; return true if the answer is ok
askquestion(title=None, message=None, **options)
Ask a question
askretrycancel(title=None, message=None, **options)
Ask if operation should be retried; return true if the answer is yes
askyesno(title=None, message=None, **options)
Ask a question; return true if the answer is yes
askyesnocancel(title=None, message=None, **options)
Ask a question; return true if the answer is yes, None if cancelled.
showerror(title=None, message=None, **options)
Show an error message
showwarning(title=None, message=None, **options)
Show a warning message
除了用tkinter的方法外,還可以用
ctypes
import ctypes
ctypes.windll.user32.MessageBoxW(0, u‘內容‘, u‘標題‘,0)
通過ctypes直接呼叫windows的API。這種方法的好處是不用安裝第三方的庫,自給自足。
pywin32
import win32api,win32con
win32api.MessageBox(0,u‘內容‘, u‘標題‘ ,win32con.MB_OK)
win32api是對windows API的封裝,用到的常量都封裝在win32con裡面。pywin32在ActivePython中有整合,一般也不用單獨安裝了。
easygui
import easygui
easygui.msgbox(u‘內容‘,u‘標題‘)
不僅僅能彈出文字內容,還可以彈出圖片內容
import easygui
easygui.msgbox(u‘內容‘,u‘標題‘,image=‘1.jpg‘)
這個庫需要單獨安裝一下 http://sourceforge.net/projects/easygui/
easygui這個庫和其他龐大的gui庫比起來就是方便,適用於一些簡單互動的場合。他不提供那種基於事件驅動的重量級解決方案,而是通過函式呼叫的方式產生一些簡單的介面。