tkFileDialog question

r rt8396 at gmail.com
Sat Nov 14 03:33:11 CET 2009


On Nov 13, 2:47 pm, "Matt Mitchell" <mmitch... at transparent.com> wrote:
> -----------------------------------
> The information contained in this electronic message and any attached document(s) is intended only for the personal and confidential use of the designated recipients named above. This message may be confidential. If the reader of this message is not the intended recipient, you are hereby notified that you have received this document in error, and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify sender immediately by telephone (603) 262-6300 or by electronic mail immediately. Thank you.
>
>
>
> -----Original Message-----
> From: python-list-bounces+mmitchell=transparent.... at python.org
>
> [mailto:python-list-bounces+mmitchell=transparent.... at python.org] On
> Behalf Of Matt Mitchell
> Sent: Friday, November 13, 2009 9:33 AM
> To: python-l... at python.org
> Subject: tkFileDialog question
>
> Hi,
>
> This is my first attempt to write a script with any kind of gui.   All I
> need the script to do is ask the user for a directory and then do stuff
> with the files in that directory.  I used tkFileDialog.askdirectory().
> It works great but it pops up an empty tk window.  Is there any way to
> prevent the empty tk window from popping up? Here's the code:
>
> import tkFileDialog
>
> answer = tkFileDialog.askdirectory()
>
> if answer is not '':
>         #do stuff
>
> Thanks!
> Matt
> --http://mail.python.org/mailman/listinfo/python-list
>
> Hi,
>
> After a few more hours of googling I answered my own question:
>
> import Tkinter, tkFileDialog
>
> root = Tk()
> root.withdraw()
>
> answer = tkFileDialog.askdirectory()
>
> if answer is not '':
>         #do stuff
>
> Thanks!!

hello Matt,

Here is one way


import Tkinter as tk
from tkFileDialog import askdirectory
import os

root = tk.Tk()
root.withdraw()

folder = askdirectory()
if folder:
    root.destroy() #make sure to do this!!!!
    print os.listdir(folder)

root.mainloop()

good luck!



More information about the Python-list mailing list