python tkinter 简单编辑器demo打开与保存文件

python tkinter 简单编辑器demo打开与保存文件

 

 

def load_file():
    with open(filename.get()) as file:

        contents.delete('1.0', END)
        contents.insert(INSERT, file.read())
def save_file():
    with open(filename.get(),'w') as file:
        file.write(contents.get('1.0',END))

 

if __name__ == '__main__':
    print_hi('good day')
    main()
    product_code = func_switch_case("bag")
    print(f"bag的产品编号:{product_code}")
    product_code = func_switch_case2("pencil")
    print(f"case2_pencil的产品编号:{product_code}")


    from tkinter import *
    from tkinter.scrolledtext import ScrolledText

    top = Tk()
    top.title('简单编辑器')
    contents = ScrolledText() #编辑框
    contents.pack(side=BOTTOM, expand= True, fill=BOTH)
    Label(text='路径:').pack(side=LEFT)
    import tkinter as aa
    def_str = aa.StringVar(value='D:\\file_test.txt')
    filename = Entry(textvariable=def_str, background='green') #路径输入框  D:\file_test.txt
    filename.pack(side=LEFT, expand=True, fill=X)

    Button(text='打开', command=load_file).pack(side=LEFT)
    Button(text ='保存', command=save_file).pack(side=LEFT)
    mainloop()

 

posted @ 2023-12-28 08:31  txwtech  阅读(54)  评论(0编辑  收藏  举报