Python程序設計之GUI(8)

1.關於文件對話框

①包含的包

import tkinter
import tkinter.ttk
#消息框
import tkinter.messagebox
#文件對話框
import tkinter.filedialog
import tkinter.colorchooser
import tkinter.scrolledtext
import tkinter.simpledialog

②對話框的初始化

    #初始化窗體
    myfiledialog=tkinter.Tk()
    myfiledialog.title("FileDialog")
    myfiledialog["width"]=500
    myfiledialog["height"]=500

③創建菜單和菜單項

    #創建菜單
    menu=tkinter.Menu(myfiledialog)
    #創建菜單項
    Filemenu=tkinter.Menu(menu,tearoff=0)
        #編輯菜單
    Editmenu=tkinter.Menu(menu,tearoff=0)
        #關於菜單
    Aboutmenu=tkinter.Menu(menu,tearoff=0)

④菜單的綁定操作

    #創建打開菜單的綁定事件處理函數
    Filemenu.add_command(label="Open",command=Open)
    Filemenu.add_command(label="Sava As",command=SaveAs)
    Filemenu.add_command(label="Close",command=Close)
    Editmenu.add_command(label="Undo",command=Undo)
	Editmenu.add_command(label="Redo",command=Undo)
    Editmenu.add_command(label="Cut",command=Cut)
    Editmenu.add_command(label="Paste",command=Paste)
    Aboutmenu.add_command(label="About",command=About)
    Editmenu.add_command(label="Search",command=Search)
    Filemenu.add_command(label="Save",command=Save)

⑤最後將菜單合併到菜單項並添加到文本框上


    menu.add_cascade(label="File",menu=Filemenu)
    menu.add_cascade(label="Edit",menu=Editmenu)
    menu.add_cascade(label="About",menu=Aboutmenu)
    #將菜單安置到窗體上
    myfiledialog.config(menu=menu)
2.源代碼

①完整代碼

import tkinter
import tkinter.ttk
import tkinter.messagebox
import tkinter.filedialog
import tkinter.colorchooser
import tkinter.scrolledtext
import tkinter.simpledialog

def main():
    #初始化窗體
    myfiledialog=tkinter.Tk()
    myfiledialog.title("FileDialog")
    myfiledialog["width"]=500
    myfiledialog["height"]=500
    #開始添加部件
    textChanger=tkinter.StringVar(value=0)
    #當前文件名
    filename=''
    #創建菜單
    menu=tkinter.Menu(myfiledialog)
    #創建菜單項
    Filemenu=tkinter.Menu(menu,tearoff=0)

    #聲明文本編輯組件
    content=tkinter.scrolledtext.ScrolledText(myfiledialog,wrap=tkinter.WORD)
    content.pack(fill=tkinter.BOTH,expand=tkinter.YES)
    def KeyPress(event):
        textChanger.set(1)
    content.bind('<KeyPress>',KeyPress)

    #操作
    def Open():
        global filename
        #內容改變就先保存內容
        if textChanger.get():
            yesno=tkinter.messagebox.askyesno(title="Save or not",message="Do you want to save?")
            if yesno==tkinter.YES:
                Save()
        filename=tkinter.filedialog.askopenfilename(title="Open a file",type=[("Python files",'*.py|*.txt')])
        if filename:
            #清空內容
            content.delete(0.0,tkinter.END)
            with open(filename,'r') as f:
                content.insert(tkinter.INSERT,''.join(f.readlines()))
            textChanger.set(0)
    #創建打開菜單的綁定事件處理函數
    Filemenu.add_command(label="Open",command=Open)

    def Save():
        global filename
        #判斷文件是否存在,不是就創建文件
        if not filename:
            SaveAs()
        elif textChanger.get():
            with open(filename,'w') as f:
                f.write(content.get(0.0,tkinter.END))
            textChanger.set(0)

    Filemenu.add_command(label="Save",command=Save)

    def SaveAs():
        global filename
        #打開另存爲窗口
        newfilename=tkinter.filedialog.asksaveasfilename(title="Save As",initialdir="C:\\",initialfile="new.txt")
        #指定文件就保存
        if newfilename:
            with open(newfilename,'w') as f:
                f.write(content.get(0.0,tkinter.END))
            textChanger.set(0)
    Filemenu.add_command(label="Sava As",command=SaveAs)

    #添加分界線
    Filemenu.add_separator()

    def Close():
        global filename
        Save()
        content.delete(0.0,tkinter.END)
        #置空文件名
        filename=''
    Filemenu.add_command(label="Close",command=Close)

    #編輯菜單
    Editmenu=tkinter.Menu(menu,tearoff=0)
    #撤銷操作
    def Undo():
        content["undo"]=True
        try:
            content.edit_undo()
        except Exception as e:
            pass
    Editmenu.add_command(label="Undo",command=Undo)
    #還原操作
    def Redo():
        content["redo"]=True
        try:
            content.edit_redo()
        except Exception as e:
            pass
    Editmenu.add_command(label="Redo",command=Undo)
    #複製操作
    def Copy():
        content.clipboard_clear()
        content.clipboard_append(content.selection_get())
    Editmenu.add_command(label="Copy",command=Copy)
    #剪切操作
    def Cut():
        Copy()
        content.delete(tkinter.SEL_FIRST,tkinter.SEL_LAST)
    Editmenu.add_command(label="Cut",command=Cut)

    #粘貼操作
    def Paste():
        #檢查是否有複製內容
        try:
            content.insert(tkinter.SEL_FIRST,content.clipboard_get())
            content.delete(tkinter.SEL_FIRST,tkinter.SEL_LAST)
        except Exception as e:
            pass
        content.insert(tkinter.INSERT,content.clipboard_get())
    Editmenu.add_command(label="Paste",command=Paste)
    Editmenu.add_separator()

    def Search():
        insearch=tkinter.simpledialog.askstring(title="Search",prompt="What to search?")
        start=content.search(insearch,0.0,tkinter.END)
        if start:
            tkinter.messagebox.showinfo(title="Found",message="OK")
    Editmenu.add_command(label="Search",command=Search)
    #關於菜單
    Aboutmenu=tkinter.Menu(menu,tearoff=0)
    def About():
        tkinter.messagebox.showinfo(title="About",message="A File Dialog!")
    Aboutmenu.add_command(label="About",command=About)

    menu.add_cascade(label="File",menu=Filemenu)
    menu.add_cascade(label="Edit",menu=Editmenu)
    menu.add_cascade(label="About",menu=Aboutmenu)
    #將菜單安置到窗體上
    myfiledialog.config(menu=menu)


    myfiledialog.mainloop()





if __name__ == '__main__':
    main()

②運行截圖
在這裏插入圖片描述

學習筆記

1.與wxPython的文件對話框初始化不一樣;
wxPython窗體文件對話框:
https://blog.csdn.net/qxyloveyy/article/details/104761393
2.是通過初始化菜單,爲菜單添加菜單項,最後將菜單綁定到窗體上。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章