1. 程式人生 > >簡單的檔案寫入與儲存 python小效果

簡單的檔案寫入與儲存 python小效果

import tkinter

# 建立一個主視窗
win = tkinter.Tk()
# 設定標題
win.title("python-1805")
# 設定視窗大小和位置
win.geometry("400x400+400+40")

e = tkinter.Variable()
entry = tkinter.Entry(win,textvariable = e)
entry.pack()

def saves():
    with open(entry.get(),'a') as f:
        f.write(text.get(0.0,tkinter.END))

button = tkinter.Button(win,
                           text = "儲存",
                        width = 20,
                        height = 5,
                        command = saves
                        )

button.pack()

def func():
    # f = open(entry.get(),"w")
    
    # # 把內容寫入緩衝區
    # f.write("www.python.org")
    # # 重新整理緩衝區  將緩衝區的內容立即寫入檔案
    # f.flush()
    # # while True:
    # #     pass
    # f.close()
    # print()
    
    with open(entry.get(),"r") as f:
        content =f.read()
        text.insert(tkinter.INSERT,content)

button2 = tkinter.Button(win,text = "開啟",width = 20,height = 5,command = func)
button2.pack()

text = tkinter.Text(win,
                    width = 200,
                    height = 50,
)
text.pack()

# 顯示主視窗
win.mainloop()