Python GUI編程學(xué)習(xí)筆記之tkinter中messagebox、filedialog控件用法詳解
本文實例講述了Python GUI編程學(xué)習(xí)筆記之tkinter中messagebox、filedialog控件用法。分享給大家供大家參考,具體如下:
相關(guān)內(nèi)容:
- messagebox
- 介紹
- 使用
- filedialog
- 介紹
- 使用
首發(fā)時間:2018-03-04 22:18
messagebox:
-
介紹:messagebox是tkinter中的消息框、對話框
-
使用:
- 導(dǎo)入模塊:
import tkinter.messagebox - 選擇消息框的模式:
- 提示消息框:【返回”ok”】
tkinter.messagebox.showinfo(消息框標(biāo)題,提示內(nèi)容)
- 消息警告框【返回”ok”】:
tkinter.messagebox.showwarning(消息框標(biāo)題,警告內(nèi)容)
- 錯誤消息框【返回”ok”】:
tkinter.messagebox.showerror(消息框標(biāo)題,錯誤提示內(nèi)容)
- 對話框:
- 詢問確認(rèn)對話框[返回”yes”,”no”]:
tkinter.messagebox.askquestion(消息框標(biāo)題,提示內(nèi)容)
- 確認(rèn)/取消對話框[返回True False]:
tkinter.messagebox.askokcancel(消息框標(biāo)題,提示內(nèi)容)
-
是/否對話框【返回True False】:
tkinter.messagebox.askyesno(消息框標(biāo)題,提示內(nèi)容)
-
重試/取消對話框:【返回值:True False】

tkinter.messagebox.askretrycancel(標(biāo)題,提示內(nèi)容)
- 詢問確認(rèn)對話框[返回”yes”,”no”]:
- 是\否\取消對話框: 【返回值:是:True 否:False 取消:None】:
tkinter.messagebox.askyesnocancel(標(biāo)題,提示內(nèi)容)
from tkinter import * import tkinter.messagebox def info_warn_err(): a=tkinter.messagebox.showinfo("我的標(biāo)題","我的提示1") print(a) a=tkinter.messagebox.showwarning("我的標(biāo)題","我的提示2") print(a) a=tkinter.messagebox.showerror("我的標(biāo)題", "我的提示3") print(a) def func2(): a=tkinter.messagebox.askyesno("我的標(biāo)題","我的提示1") print(a) a=tkinter.messagebox.askokcancel("我的標(biāo)題","我的提示2") print(a) a=tkinter.messagebox.askquestion("我的標(biāo)題","我的提示3") print(a) a=tkinter.messagebox.askretrycancel("我的標(biāo)題","我的提示4") print(a) a=tkinter.messagebox.askyesnocancel("我的標(biāo)題","我的提示5") print(a) #這里用作演示如何使用對話框 if tkinter.messagebox.askyesno("我的標(biāo)題", "確認(rèn)關(guān)閉窗口嗎!"): root.destroy() root=Tk() btn=Button(root,text="信息、警告、錯誤消息框",command=info_warn_err) btn1=Button(root,text="對話框",command=func2) btn.pack() btn1.pack() root.mainloop()
filedialog:
- 介紹:filedialog是tkinter中的文件對話框

- 使用:
- 導(dǎo)入模塊:import tkinter.filedialog
- 選擇文件對話框的格式:
- tkinter.filedialog.asksaveasfilename():選擇以什么文件名保存,返回文件名
- tkinter.filedialog.asksaveasfile():選擇以什么文件保存,創(chuàng)建文件并返回文件流對象
- tkinter.filedialog.askopenfilename():選擇打開什么文件,返回文件名
- tkinter.filedialog.askopenfile():選擇打開什么文件,返回IO流對象
- tkinter.filedialog.askdirectory():選擇目錄,返回目錄名
- tkinter.filedialog.askopenfilenames():選擇打開多個文件,以元組形式返回多個文件名
- tkinter.filedialog.askopenfiles():選擇打開多個文件,以列表形式返回多個IO流對象
import tkinter.filedialog from tkinter import * def func1(): a=tkinter.filedialog.asksaveasfilename()#返回文件名 print(a) a =tkinter.filedialog.asksaveasfile()#會創(chuàng)建文件 print(a) a =tkinter.filedialog.askopenfilename()#返回文件名 print(a) a =tkinter.filedialog.askopenfile()#返回文件流對象 print(a) a =tkinter.filedialog.askdirectory()#返回目錄名 print(a) a =tkinter.filedialog.askopenfilenames()#可以返回多個文件名 print(a) a =tkinter.filedialog.askopenfiles()#多個文件流對象 print(a) root=Tk() btn1=Button(root,text="click",command=func1) btn1.pack() root.mainloop()
- 介紹:filedialog是tkinter中的文件對話框
- 提示消息框:【返回”ok”】
- 導(dǎo)入模塊:
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python3.6+selenium實現(xiàn)操作Frame中的頁面元素
這篇文章主要為大家詳細(xì)介紹了python3.6+selenium實現(xiàn)操作Frame中的頁面元素,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
django-rest-swagger對API接口注釋的方法
今天小編就為大家分享一篇django-rest-swagger對API接口注釋的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Python基于datetime或time模塊分別獲取當(dāng)前時間戳的方法實例
今天小編就為大家分享一篇關(guān)于Python基于datetime或time模塊分別獲取當(dāng)前時間戳的方法實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02
Python使用matplotlib實現(xiàn)基礎(chǔ)繪圖功能示例
這篇文章主要介紹了Python使用matplotlib實現(xiàn)基礎(chǔ)繪圖功能,結(jié)合實例形式分析了Python基于matplotlib實現(xiàn)正弦、余弦圖形及多軸圖的相關(guān)繪制操作技巧,需要的朋友可以參考下2018-07-07

