JFileChooser for Python?

こ雲淡風輕ζ 提交于 2020-07-08 13:32:52

问题


I was wondering if there is something similar to Java's JFileChooser for Python?

JFileChooser is a graphical front end to choose a file.

Preferably something that is already with Python. Maybe with Tkinter.


回答1:


For something that doesn't require wxPython and sticks with the standard Python libs, you can use the tkFileDialog.askopenfilename() method:

#!/usr/bin/python

from Tkinter import *
from tkFileDialog import askopenfilename

root = Tk()
root.withdraw()
print askopenfilename()



回答2:


wxPython (www.wxpython.org) provides the wx.FileDialog class which will give you a native file selection dialog on any of the supported platforms (Mac, Linux or Windows).




回答3:


Easiest way I ever found to do this (using PyGTK and Kiwi):

from kiwi.ui.dialogs import open as open_dialog

chosen_path = open_dialog('Select a file', folder='/start/folder')

if chosen_path is not None:
    # do something ...



回答4:


That would depend on your windowing toolkit. wxWidgets provides the wxFileDialog.




回答5:


For python 3 what you're looking for is tkinter.filedialog, and all that comes with it. Here's a short program that opens and then prints a TXT file of the user's choosing via askopenfilename:

from tkinter import *
from tkinter.filedialog import askopenfilename

root = Tk()
root.withdraw()
root.update()
pathString = askopenfilename(filetypes=[("Text files","*.txt")])
if pathString:
    openFile = open(pathString, 'r')
    fileString = openFile.read()
    print(fileString)
root.destroy()

Output is whatever is in the selected file.




回答6:


Maybe you would like to take a look at Jython.



来源:https://stackoverflow.com/questions/433945/jfilechooser-for-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!