试试这个:import os
from tkinter import *
from tkinter import filedialog
def inPut():
opendir = filedialog.askdirectory(parent=root,initialdir="/",title='Input Folder')
inPut_dir.set (os.path.abspath (opendir))
root = Tk()
root.geometry("640x240")
root.title("Settings")
frametop = Frame(root)
framebottom = Frame(root)
frameright = Frame(framebottom)
inPut_dir = StringVar ()
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)
if os.path.isfile ("path.txt"):
with open ("path.txt") as f: inPut_dir.set (f.read ())
else: inPut_dir.set ("")
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 ()
with open ("path.txt", "w") as f: f.write (inPut_dir.get ())
编辑:
如果您对选择多个目录感兴趣,您可以查看另一个stack overflow问题的this链接。在