Python:GUI之tkinter學習筆記之messagebox、filedialog


 


相關內容:

  • messagebox
    • 介紹
    • 使用
  • filedialog
    • 介紹
    • 使用

 

首發時間:2018-03-04 22:18


messagebox:

 

  • 介紹:messagebox是tkinter中的消息框、對話框

  • 使用:

    • 導入模塊:import tkinter.messagebox
    • 選擇消息框的模式:
      • 提示消息框:【返回”ok”】image
        tkinter.messagebox.showinfo(消息框標題,提示內容)
      • 消息警告框【返回”ok”】:image
        tkinter.messagebox.showwarning(消息框標題,警告內容)
      • 錯誤消息框【返回”ok”】:image[9]
        tkinter.messagebox.showerror(消息框標題,錯誤提示內容)
      • 對話框:
        • 詢問確認對話框[返回”yes”,”no”]:image
          tkinter.messagebox.askquestion(消息框標題,提示內容)
        • 確認/取消對話框[返回True False]:image
          tkinter.messagebox.askokcancel(消息框標題,提示內容)
        • 是/否對話框【返回True False】:image

          tkinter.messagebox.askyesno(消息框標題,提示內容)

        • 重試/取消對話框:【返回值:True False】image

          tkinter.messagebox.askretrycancel(標題,提示內容)
        • 是\否\取消對話框: 【返回值:是:True  否:False 取消:None】:
          tkinter.messagebox.askyesnocancel(標題,提示內容)

 

from tkinter import *
import tkinter.messagebox
def info_warn_err():
    a=tkinter.messagebox.showinfo("我的標題","我的提示1")
    print(a)
    a=tkinter.messagebox.showwarning("我的標題","我的提示2")
    print(a)
    a=tkinter.messagebox.showerror("我的標題", "我的提示3")
    print(a)
def func2():
    a=tkinter.messagebox.askyesno("我的標題","我的提示1")
    print(a)
    a=tkinter.messagebox.askokcancel("我的標題","我的提示2")
    print(a)
    a=tkinter.messagebox.askquestion("我的標題","我的提示3")
    print(a)
    a=tkinter.messagebox.askretrycancel("我的標題","我的提示4")
    print(a)
    a=tkinter.messagebox.askyesnocancel("我的標題","我的提示5")
    print(a)
    #這里用作演示如何使用對話框
    if tkinter.messagebox.askyesno("我的標題", "確認關閉窗口嗎!"):
        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中的文件對話框image
  • 使用:
    • 導入模塊:import tkinter.filedialog
    • 選擇文件對話框的格式:
      • tkinter.filedialog.asksaveasfilename():選擇以什么文件名保存,返回文件名
      • tkinter.filedialog.asksaveasfile():選擇以什么文件保存,創建文件並返回文件流對象
      • 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()#會創建文件
    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()

 

 



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM