尝试导入 tkFileDialog:
import tkFileDialog as tkfd
def save():
savenm = tkfd.asksaveasfile()
f = open(savenm.name,"w")
# then put what to do with the opened file
def open():
opennm = tkfd.askopenfile()
f = open(savenm.name,"r")
# then put what to do with the opened file
然后制作一个使用以下功能的按钮:
import Tkinter as tk
root=tk.Tk()
SAVELOADFRAME = tk.Frame(root)
SAVELOADFRAME.pack()
savebtn = Button(SAVELOADFRAME,text="Save",command=save)
savebtn.pack(side=LEFT)
root.mainloop()
loadbtn = Button(SAVELOADFRAME,text="Open",command=open)
loadbtn.pack(side=RIGHT)
也许如果您有一个记事本框,您可能希望将文件中的文本插入到 tk.Text 小部件中。上面的代码仅适用于基于文本的文件(例如 *.js、*.txt、*.py),不适用于 *.exe、*.dll 等。
希望能解决你的问题:^)