12

我正在尝试创建一个带有浏览窗口的 GUI 来定位特定文件。我之前发现了这个问题:Browsing file or directory Dialog in Python

虽然当我查看这些条款时,它似乎并不是我想要的。

我需要的只是可以从 Tkinter 按钮启动的东西,该按钮从浏览器返回所选文件的路径。

有人有这方面的资源吗?

编辑:好的,所以问题已经得到解答。对于任何有类似问题的人,请进行研究,那里的代码确实有效。不要在 cygwin 中测试它。由于某种原因,它在那里不起作用。

4

6 回答 6

18

我认为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

编辑:此链接有更多示例

于 2013-11-13T03:41:17.430 回答
5

我重新编写了Roberto 的代码,但在Python3中重新编写了(只是微小的变化)。

您可以按原样复制和粘贴一个简单的演示 .py 文件,或者只是复制函数“ search_for_file_path ”(以及相关的导入)并作为函数放入您的程序中。

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)
于 2019-03-11T10:37:39.153 回答
4

这将生成一个只有一个名为“浏览”按钮的 GUI,它会打印出您从浏览器中选择的文件路径。可以通过更改代码段<*.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()
于 2014-07-09T08:38:13.080 回答
4

在 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
于 2018-07-17T00:18:50.017 回答
3

基于先前的答案和在此线程中找到的答案:How to give Tkinter file dialog focus here is a quick way to pull up a file selector in Python 3 without see the tinker window, and also pull the browser window to the front of屏幕

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 )
于 2019-08-15T16:31:17.847 回答
1

使用文件名:

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() 
于 2021-03-18T20:55:35.383 回答