簡單的檔案寫入與儲存 python小效果
阿新 • • 發佈:2018-11-19
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: #獲取0行和0列,結束 f.write(text.get(0.0,tkinter.END)) button = tkinter.Button(win, text = "儲存", width = 20, height = 5, command = saves ) button.pack() def func(): with open(entry.get(),"r") as f: content =f.read() #text.insert(index,string) index = x.y的形式,x表示行,y表示列 #向第一行插入資料,text.insert(1.0,'hello world') 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()