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

來源:互聯網
上載者:User

標籤:時間   question   mes   main   提示   inf   images   pytho   comm   

 

相關內容:
  • messagebox
    • 介紹
    • 使用
  • filedialog
    • 介紹
    • 使用

 

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

messagebox:

 

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

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

        • 重試/取消對話方塊:【傳回值:True False】

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

 

from tkinter import *import tkinter.messageboxdef 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中的檔案對話方塊
  • 使用:
    • 匯入模組: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.filedialogfrom 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()

 

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.