Browse for file path in python
我正在尝试创建一个带有浏览窗口的GUI,以查找特定文件。
我之前发现了这个问题:在Python
中浏览文件或目录对话框
尽管当我查找这些术语时,似乎并不是我想要的。
我需要的是可以从Tkinter按钮启动的东西,该按钮可以从浏览器返回所选文件的路径。
有人为此有资源吗?
编辑:好的,这样的问题已经回答了。对于任何有类似问题的人,请您进行研究,这里的代码确实起作用。不要在cygwin中对其进行测试。由于某种原因,它在那里不起作用。
我认为TkFileDialog对您可能有用。
1 2 3 4 5 6 7 8 9 10 11 | 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 |
编辑:此链接有更多示例
这将生成仅带有一个名为"浏览"按钮的GUI,该GUI会打印出您从浏览器中选择的文件路径。可以通过更改代码段<*。type>来指定文件的类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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() |
我重新制作了Roberto的代码,但是用Python3进行了重写(只是微小的更改)。
您可以照原样复制并粘贴一个简单的演示.py文件,也可以仅复制函数" search_for_file_path"(以及相关的导入),然后将其作为函数放入程序中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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 ("\ file_path_variable =", file_path_variable) |
在python 3中,它已重命名为filedialog。您可以通过askdirectory方法(事件)访问文件夹传递,如下所示。如果要选择文件路径,请使用askopenfilename
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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的属性,即
1 | main_win.sourceFolder |
以先前的答案和在该线程中找到的答案为基础:如何在这里使Tkinter文件对话框成为焦点,是一种快速的方法,可以在Python 3中拉起文件选择器而无需查看修补窗口,也可以将浏览窗口拉至屏幕正面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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 ) |
使用file.name:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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() |