我有一个应用程序,它通过askdirectory打开一个文件夹,并显示在GUI的一个输入框中,我的问题是如何将打开的文件夹保存到文件中,并在程序重新打开时再次调用它?在
我的代码:
import os
from tkinter import *
from tkinter import filedialog
inPut_dir = ''
def inPut():
opendir = filedialog.askdirectory(parent=root,initialdir="/",title='Input Folder')
inPut_dir = StringVar()
inPut_dir = os.path.abspath(opendir)
entry.delete(0, END)
entry.insert(0, inPut_dir)
root = Tk()
root.geometry("640x240")
root.title("Settings")
frametop = Frame(root)
framebottom = Frame(root)
frameright = Frame(framebottom)
text = Label(frametop, text="Input Folder").grid(row=5, column=2)
entry = Entry(frametop, width=50, textvariable=inPut_dir)
entry.grid(row=5,column=4,padx=2,pady=2,sticky='we',columnspan=20)
ButtonA = Button(frametop, text="Change", command=inPut).grid(row=5, column=28)
ButtonB = Button(frameright, text="OK").grid(row=5, column=20, padx=10)
ButtonC = Button(frameright, text="Cancel").grid(row=5, column=15)
frametop.pack(side=TOP, fill=BOTH, expand=1)
framebottom.pack(side=BOTTOM, fill=BOTH, expand=1)
frameright.pack(side=RIGHT)
root.mainloop()
您可以使用open()函数。在
您可以定义包装器函数,例如:
{{cd2>要保存一个名为^ cd2>的变量:
^{pr2}$以后从文件读取:
试试这个:
编辑:
如果您对选择多个目录感兴趣,您可以查看另一个
stack overflow
问题的this链接。在相关问题 更多 >
编程相关推荐