Skip to content
Advertisement

have a pre-loaded directory when using tkinter.filedialog.askdirectory()

I have the following code:

imports:

from tkinter import *
from tkinter.filedialog import askopenfilename, askdirectory

main:

master = Tk()
Label(master, text="Output dir").grid(row=1, column=0 ,sticky=W)
with open('{}\resources\last_saved_dir.config'.format(path_of_main_project), 'r') as saved_dir:
    last_dir = saved_dir.read().replace('n', '')
entry_dir=Entry(master, text=last_dir, width=50)
entry_dir.grid(row=1, column=1, sticky=W, padx=5)
Button(master, text="Browse...", width=10, command=lambda:get_ouput_directory(entry_dir)).grid(row=1, column=2, sticky=W)

# The following part is after directory is chosen, runs the rest of the program. 
# Not important for my question, just showing that I run and close my loop.

Button(master, text="Ok",     command=run_and_close, width=10).grid(row=3, column=1, sticky=E, padx=5)
Button(master, text="Cancel", command=close, width=10).grid(row=3, column=2, sticky=W)

master.bind('<Return>', run_and_close)
master.bind('<Escape>', close)

mainloop()

This creates a gui window that prompts the user to enter a directory, with the text Output dir and then an open entry window with a Browse button to load the directory

The following is get_ouput_directory() function:

def get_ouput_directory(directory_entry):
    dir_name = askdirectory(title = "SiteConverter output directory")
    directory_entry.delete(0,END)
    directory_entry.insert(0,dir_name)
    with open('{}\resources\last_saved_dir.config'.format(path_of_main_project), 'w') as saved_dir:
        saved_dir.write(dir_name)

The idea is:

  1. First time user runs this, the entry window is empty and the user would browse for a directory, which is then saved inside a certain folder in a file called last_saved_dir.config
  2. Second time user runs the program, the program would read that file and load the directory inside the window so the user doesn’t have to manually input it every time if the directory is unchanged.

Note that the program doesn’t start reading the file from second run, it would always read, but the first time would either be an unwanted directory or blank if the user is running it for the first time. However every time I run the script the entry window is blank even though the last directory is saved inside last_saved_dir.config. When I add print(last_dir) just before entry_dir=Entry(master, text=last_dir, width=50) the last directory is read correctly and the the type is <class 'str'> which should be good for text= ...

Advertisement

Answer

I’m not really sure what the text= option of an Entry does, since it isn’t mentioned in any reference. To put text in an entry use the .insert() method:

entry_dir.insert(END, last_dir)

Then, to always open the askdirectory window with the folder currently in the Entry, use

dir_name = askdirectory(title = "SiteConverter output directory", initialdir=directory_entry.get())

As per Bryan Oakley’s comments, Tkinter accepts abbreviations of option names as long as they are unique. The Entry widget doesn’t have a text option, but it does have a textvariable option so that’s the one you set when you set text=.... That makes that if you were to use it, you shouldn’t give it a string object but a StingVar object. For this specific example you’re good without it though.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement