qwww
qwww

Reputation: 1363

How to select a directory and store it into a variable in tkinter?

I have tkinter GUI. I like to have a textbox and a button. On clicking that button a file explorer should open and finally a directory can be selected. This path has to be shown in the textbox.

code :

from tkinter import *
from tkinter import ttk
from tkinter import filedialog
gui = Tk()
gui.geometry("400x400")
gui.title("FC")
a = Label(gui ,text="Enter name").grid(row=0,column = 0)
E = Entry(gui).grid(row=0,column=1)
folder_selected = filedialog.askdirectory()
c = ttk.Button(gui ,text="find").grid(row=4,column=0)
gui.mainloop()

Upvotes: 4

Views: 17693

Answers (1)

scotty3785
scotty3785

Reputation: 7006

How about something like this

from tkinter import *
from tkinter import ttk
from tkinter import filedialog
gui = Tk()
gui.geometry("400x400")
gui.title("FC")

def getFolderPath():
    folder_selected = filedialog.askdirectory()
    folderPath.set(folder_selected)

def doStuff():
    folder = folderPath.get()
    print("Doing stuff with folder", folder)

folderPath = StringVar()
a = Label(gui ,text="Enter name")
a.grid(row=0,column = 0)
E = Entry(gui,textvariable=folderPath)
E.grid(row=0,column=1)
btnFind = ttk.Button(gui, text="Browse Folder",command=getFolderPath)
btnFind.grid(row=0,column=2)

c = ttk.Button(gui ,text="find", command=doStuff)
c.grid(row=4,column=0)
gui.mainloop()

Summary:

  • You were creating your widgets and using the layout grid on the same line. This is a bad idea. I've split it out so that you define the button and then do the layout on a new line
  • I've added a Stringvar, this is a special tkinter variable that can be used to automatically update fields
  • I've added two functions, one is called when you press the new browse button and will bring up the folder select dialog. I've added another for the "Find" button which you can use for what ever you want to do with the folder path.

Consider: More descriptive naming. Note that I've called the new button btnFind rather than a single character like you have done for the other widgets. This will make debugging/understanding your code easier when you come back to look at it.

EDIT: Since you want more than 1 of these folder select items, it is worth creating a class that contains all the properties and functions for selecting a folder. Here is an example

from tkinter import *
from tkinter import ttk
from tkinter import filedialog
gui = Tk()
gui.geometry("400x400")
gui.title("FC")

class FolderSelect(Frame):
    def __init__(self,parent=None,folderDescription="",**kw):
        Frame.__init__(self,master=parent,**kw)
        self.folderPath = StringVar()
        self.lblName = Label(self, text=folderDescription)
        self.lblName.grid(row=0,column=0)
        self.entPath = Entry(self, textvariable=self.folderPath)
        self.entPath.grid(row=0,column=1)
        self.btnFind = ttk.Button(self, text="Browse Folder",command=self.setFolderPath)
        self.btnFind.grid(row=0,column=2)
    def setFolderPath(self):
        folder_selected = filedialog.askdirectory()
        self.folderPath.set(folder_selected)
    @property
    def folder_path(self):
        return self.folderPath.get()

def doStuff():
    folder1 = directory1Select.folder_path
    folder2 = directory2Select.folder_path
    folder3 = directory3Select.folder_path
    print("Doing stuff with folder", folder1, folder2, folder3)

folderPath = StringVar()

directory1Select = FolderSelect(gui,"Select Folder 1")
directory1Select.grid(row=0)

directory2Select = FolderSelect(gui,"Select Folder 2")
directory2Select.grid(row=1)

directory3Select = FolderSelect(gui,"Select Folder 3")
directory3Select.grid(row=2)


c = ttk.Button(gui, text="find", command=doStuff)
c.grid(row=4,column=0)
gui.mainloop()

You can now have as many different Folder Selects as you want.

Upvotes: 16

Related Questions