我正在尝试使用浏览窗口创建GUI以查找特定文件。 我之前发现了这个问题:Browsing file or directory Dialog in Python
虽然当我查看条款时,它似乎并不是我想要的。
我需要的只是可以从Tkinter按钮启动的东西,它从浏览器返回所选文件的路径。
有人有资源吗?
编辑:好的,所以问题已得到解答。对于任何有类似问题的人,做你的研究,那里的代码都可以。不要在cygwin中测试它。由于某种原因它在那里不起作用。答案 0 :(得分:10)
我认为TkFileDialog可能对您有用。
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
编辑:this link还有一些例子
答案 1 :(得分:3)
这将生成一个GUI,其中只有一个名为' Browse'的按钮,它打印出您从浏览器中选择的文件路径。可以通过更改代码段< * .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()
答案 2 :(得分:0)
在python 3中,它已重命名为filedialog。您可以通过 askdirectory 方法(事件)访问文件夹传递,如下所示。如果要选择文件路径,请使用 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 )
注意:即使关闭main_win之后,变量的值仍然存在。但是,您需要将该变量用作main_win的属性,即
main_win.sourceFolder
答案 3 :(得分:0)
我重新制作了Roberto's代码,但是用 Python3 进行了重写(只是微小的变化)。
您可以照原样复制并粘贴一个简单的演示.py文件,或仅复制函数“ search_for_file_path ”(以及相关的导入),然后将其作为函数放入程序中。 / p>
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)
答案 4 :(得分:0)
以先前的答案和在该线程中找到的答案为基础:How to give Tkinter file dialog focus是一种快速的方法,可以在Python 3中拉出文件选择器而不看到修补窗口,也可以将浏览窗口拉到最前面屏幕的
import tkinter from tkinter
import messagebox 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 )
答案 5 :(得分:0)
使用文件名:
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()