pythonmsgbox怎么使用_如何使用tkinter的messagebox
tkinter模塊是Python自帶的編寫GUI程序的模塊,messagebox是很多GUI程序都會用到的與用戶交互的消息對話框。messagebox出現后,對應的線程會阻塞,直到用戶回應。本文介紹tkinter模塊中8種標準messagebox的使用。
我們要寫一點測試代碼,來查看8種標準messagebox的形態,以及點擊不同的按鈕(用戶回應)之后的返回值。
showinfo
簡單的顯示信息。
>>> from tkinter import messagebox as msgbox
>>> print(msgbox.showinfo('i\'m showinfo','Now you are at www.pynote.net! Welcome...:)'))
ok
showinfo
第1個參數指定對話框左上角的title,第2個參數指定窗體中的內容,這個規則適合所有的messagebox。messagebox窗口的整體風格,會根據代碼運行環境而出現變化。本文測試代碼運行截圖的環境是Win系統下帶X Server的SSH客戶端,連接Ubuntu桌面。
showinfo只有1個按鈕,OK,點擊OK,函數的返回也是ok字符串。直接關閉窗口,也是返回ok。
showwarning
>>> print(msgbox.showwarning('i\'m showwarning','This is a warning...'))
ok
showwarning
與showinfo不一樣的地方是,默認的圖標發生了變化,點擊OK,返回也是ok字符串。直接關閉窗口,同樣返回ok。因為這只是info,用戶關閉此窗口,表示他看到了此信息,效果等同于點擊OK按鈕。
showerror
show...系列的最后一個messagebox:
>>> print(msgbox.showerror('i\'m showerror','Careful! There is an error...'))
ok
showerror
同樣點擊按鈕,以及關閉窗口,返回小寫的ok字符串。
askquestion
二選一的問題窗口:
>>> print(msgbox.askquestion('i\'m askquestion','what if we go to Mars?'))
yes
>>> print(msgbox.askquestion('i\'m askquestion','what if we go to Mars?'))
no
askquestion
兩個按鈕,Yes返回yes,No返回no。直接關閉窗口,返回no。
askokcancel
確定或取消的窗口:
>>> print(msgbox.askokcancel('i\'m askokcancel','what if we go to Moon?'))
True
>>> print(msgbox.askokcancel('i\'m askokcancel','what if we go to Moon?'))
False
askokcancel
兩個按鈕,OK返回True,Cancel返回False。直接關閉窗口返回False。
askyesno
是,或否,適用這個窗口:
>>> print(msgbox.askyesno('i\'m askyesno','what if we dinner together?'))
True
>>> print(msgbox.askyesno('i\'m askyesno','what if we dinner together?'))
False
askyesno
兩個按鈕,Yes返回True,No返回False。與askquestion函數不一樣的僅僅是返回值,這個細節可能會讓默寫代碼的可讀性更好。直接關閉窗口,返回False。
askyesnocancel
是,或否,還可以不回答,適合這個窗口:
>>> print(msgbox.askyesnocancel('i\'m askyesnocancel','would you marry me?'))
True
>>> print(msgbox.askyesnocancel('i\'m askyesnocancel','would you marry me?'))
False
>>> print(msgbox.askyesnocancel('i\'m askyesnocancel','would you marry me?'))
None
askyesnocancel
三個按鈕,Yes返回True,No返回False,Cancel返回None。直接關閉窗口,返回None。
askretrycancel
詢問要不要重來,適合這個消息窗口:
>>> print(msgbox.askretrycancel('i\'m askretrycancel','Again?'))
True
>>> print(msgbox.askretrycancel('i\'m askretrycancel','Again?'))
False
askretrycancel
兩個按鈕,Retry返回True,Cancel返回False。直接關閉窗口,返回False。
以上就是對tkinter的8種messagebox的介紹,希望喜歡。
-- EOF --
總結
以上是生活随笔為你收集整理的pythonmsgbox怎么使用_如何使用tkinter的messagebox的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python with语句_python
- 下一篇: httpclient base64 文件