简单的文件写入与保存 python小效果

版权声明:xuhaha©2018 https://blog.csdn.net/weixin_42793426/article/details/82850639
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()

猜你喜欢

转载自blog.csdn.net/weixin_42793426/article/details/82850639