Selecting a file in a directory

vsoler vicente.soler at gmail.com
Sat Feb 13 21:00:03 EST 2010


On Feb 14, 2:45 am, rantingrick <rantingr... at gmail.com> wrote:
> On Feb 13, 7:28 pm, "Alf P. Steinbach" <al... at start.no> wrote:
>
>
>
> > * vsoler:
>
> > > Hi,
>
> > > My python script needs to work with a .txt file in a directory. I
> > > would like to give the user the possibility to choose the file he
> > > needs to work on in as much the same way as I open a .xls file in
> > > Excel, that is, I want to make appear the "Windows'" window and let
> > > the user choose.
>
> > > I think this should be quite straightforward.
>
> > > How should I proceed?
>
> > At least in Windows one easy way is to delegate that responsibility to the
> > Windows shell. When a user drags a file onto your script, your script is run
> > with the path to that dragged file as argument. Or it can even be multiple files.
>
> > Otherwise, tkinter has, as I recall, a standard file chooser dialog.
>
> > These "standard" dialogs are generally called "common dialogs".
>
> specifically Alf was referring to tkFileDialog.askopenfilename().
> Heres an example...
>
> import Tkinter as tk
> from tkFileDialog import askopenfilename
>
> root = tk.Tk()
>
> def get_files():
>     path = askopenfilename(filetypes=[('TXT', '.txt')])
>     if path:
>         print path
>
> tk.Button(root, text='1', font=('Wingdings', 12),
> command=get_files).pack(padx=5, pady=5)
>
> root.mainloop()

Excellent!!! Just what I needed!



More information about the Python-list mailing list