在python中浏览文件路径 [英] Browse for file path in python
问题描述
我正在尝试创建一个带有浏览窗口的GUI,以查找特定文件. 我之前发现了这个问题:在Python中浏览文件或目录对话框
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.
我需要的是可以从Tkinter按钮启动的东西,该按钮可以从浏览器返回所选文件的路径.
All I need is something launchable from a Tkinter button that returns a the path of the selected file from the browser.
有人为此有资源吗?
好的,这样的问题已经回答了.对于任何有类似问题的人,请您进行研究,这里的代码确实起作用.不要在cygwin中对其进行测试.由于某些原因,它在那里不起作用.
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.
推荐答案
我认为 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
此链接有更多示例
这篇关于在python中浏览文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!