13

I am trying to create a GUI with a browse window to locate a specific file. I found this question earlier: Browsing file or directory Dialog in Python

although when I looked up the terms it didn't seem to be what I was looking for.

All I need is something launchable from a Tkinter button that returns a the path of the selected file from the browser.

Anybody have a resources for this?

EDIT: Alright so the question has been answered. To anybody with a similar question, do your research, the code out there DOES work. DO NOT test it in cygwin. it doesn't work in there for some reason.

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Funkyguy
  • 628
  • 2
  • 10
  • 31

6 Answers6

22

I think TkFileDialog might be useful for you.

import Tkinter
import tkFileDialog
import os

root = Tkinter.Tk()
root.withdraw() #use to hide tkinter window

currdir = os.getcwd()
tempdir = tkFileDialog.askdirectory(parent=root, initialdir=currdir, title='Please select a directory')
if len(tempdir) > 0:
    print "You chose %s" % tempdir

EDIT: this link has some more examples

Roberto
  • 2,054
  • 4
  • 31
  • 46
  • 2
    Awesome! Thank you! Note to anybody who finds this, don't test it in cygwin! You'll get some $DISPLAY environment variable error. Its cygwins fault, not the code. – Funkyguy Nov 13 '13 at 16:15
  • 1
    You presumably need an X server running for it to work in cygwin. – Evan Nov 13 '13 at 16:57
  • 2
    I'm getting the error `ModuleNotFoundError: No module named 'tkFileDialog'`. Is this function no longer included in python 3.7? – Kes Perron Nov 08 '19 at 20:22
  • 3
    @SMPerron In Python 3.x use ```import tkinter.filedialog as fd``` or something similar. I'm sure you know how import works. – Nummer_42O Nov 20 '19 at 12:57
10

I remade Roberto's code, but rewritten in Python3 (just minor changes).

You can copy-and-paste as is for an easy demonstration .py file, or just copy the function "search_for_file_path" (and associated imports) and place into your program as a function.

import tkinter
from tkinter import filedialog
import os

root = tkinter.Tk()
root.withdraw() #use to hide tkinter window

def search_for_file_path ():
    currdir = os.getcwd()
    tempdir = filedialog.askdirectory(parent=root, initialdir=currdir, title='Please select a directory')
    if len(tempdir) > 0:
        print ("You chose: %s" % tempdir)
    return tempdir


file_path_variable = search_for_file_path()
print ("\nfile_path_variable = ", file_path_variable)
Shane Rooney
  • 99
  • 1
  • 2
5

In python 3 it has been renamed to filedialog. you can access a folder pass by askdirectory method(event) as follows. If you want to choose a file path use askopenfilename

import tkinter 
from tkinter import messagebox
from tkinter import filedialog

main_win = tkinter.Tk()
main_win.geometry("1000x500")
main_win.sourceFolder = ''
main_win.sourceFile = ''
def chooseDir():
    main_win.sourceFolder =  filedialog.askdirectory(parent=main_win, initialdir= "/", title='Please select a directory')

b_chooseDir = tkinter.Button(main_win, text = "Chose Folder", width = 20, height = 3, command = chooseDir)
b_chooseDir.place(x = 50,y = 50)
b_chooseDir.width = 100


def chooseFile():
    main_win.sourceFile = filedialog.askopenfilename(parent=main_win, initialdir= "/", title='Please select a directory')

b_chooseFile = tkinter.Button(main_win, text = "Chose File", width = 20, height = 3, command = chooseFile)
b_chooseFile.place(x = 250,y = 50)
b_chooseFile.width = 100

main_win.mainloop()
print(main_win.sourceFolder)
print(main_win.sourceFile )

Note: the value of variables persist even after closing the main_win. However, you need to use the variable as an attribute of the main_win i.e.

main_win.sourceFolder
Afshin Amiri
  • 3,438
  • 1
  • 20
  • 21
4

This will generate a GUI with just a button called 'Browse', which prints out the file path that you choose from the browser. The type of the file can be specified by changing the code segment <*.type>.

from Tkinter import * 
import tkFileDialog

import sys
if sys.version_info[0] < 3:
   import Tkinter as Tk
else:
   import tkinter as Tk


def browse_file():

fname = tkFileDialog.askopenfilename(filetypes = (("Template files", "*.type"), ("All files", "*")))
print fname

root = Tk.Tk()
root.wm_title("Browser")
broButton = Tk.Button(master = root, text = 'Browse', width = 6, command=browse_file)
broButton.pack(side=Tk.LEFT, padx = 2, pady=2)

Tk.mainloop()
Cugomastik
  • 911
  • 13
  • 22
4

Building on the previous answers and an answer found in this thread: How to give Tkinter file dialog focus here is a quick way to pull up a file selector in Python 3 without seeing the tinker window, and also pull the browse window to the front of screen

import tkinter
from tkinter import filedialog

#initiate tinker and hide window 
main_win = tkinter.Tk() 
main_win.withdraw()

main_win.overrideredirect(True)
main_win.geometry('0x0+0+0')

main_win.deiconify()
main_win.lift()
main_win.focus_force()

#open file selector 
main_win.sourceFile = filedialog.askopenfilename(parent=main_win, initialdir= "/",
title='Please select a directory')

#close window after selection 
main_win.destroy()

#print path 
print(main_win.sourceFile )
Foggy
  • 383
  • 2
  • 12
1

use file.name:

from tkinter import * 
from tkinter.ttk import *
from tkinter.filedialog import askopenfile 

root = Tk() 
root.geometry('700x600')

def open_file(): 
    file = askopenfile(mode ='r', filetypes =[('Excel Files', '*.xlsx')])
    if file is not None: 
        print(file.name)
     

btn = Button(root, text ='Browse File Directory', command =lambda:open_file())
btn.pack(side = TOP, pady = 10) 

mainloop()