Reputation: 620
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.
Upvotes: 13
Views: 117914
Reputation: 11
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()
Upvotes: 1
Reputation: 41
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 )
Upvotes: 4
Reputation: 99
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)
Upvotes: 10
Reputation: 3583
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
Upvotes: 5
Reputation: 1009
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()
Upvotes: 4
Reputation: 2194
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
Upvotes: 22