Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browse a Windows directory GUI using Python 2.7

Tags:

python

I'm using Windows XP with Python 2.7.2 & Tkinter GUI kit. I want to build a simple GUI that has a text field and "Browse" button that will select a file through directories such as C:\ (Just like Windows Explorer). That file selected will be displayed in the text field in the GUI. Hope this is descriptive enough.

like image 870
guiNachos Avatar asked Feb 22 '23 07:02

guiNachos


1 Answers

I have something else that might help you:

    ## {{{ http://code.activestate.com/recipes/438123/ (r1)
    # ======== Select a directory:

    import Tkinter, tkFileDialog

    root = Tkinter.Tk()
    dirname = tkFileDialog.askdirectory(parent=root,initialdir="/",title='Please select a directory')
    if len(dirname ) > 0:
        print "You chose %s" % dirname 


    # ======== Select a file for opening:
    import Tkinter,tkFileDialog

    root = Tkinter.Tk()
    file = tkFileDialog.askopenfile(parent=root,mode='rb',title='Choose a file')
    if file != None:
        data = file.read()
        file.close()
        print "I got %d bytes from this file." % len(data)


    # ======== "Save as" dialog:
    import Tkinter,tkFileDialog

    myFormats = [
        ('Windows Bitmap','*.bmp'),
        ('Portable Network Graphics','*.png'),
        ('JPEG / JFIF','*.jpg'),
        ('CompuServer GIF','*.gif'),
        ]

    root = Tkinter.Tk()
    fileName = tkFileDialog.asksaveasfilename(parent=root,filetypes=myFormats ,title="Save the image as...")
    if len(fileName ) > 0:
        print "Now saving under %s" % nomFichier
    ## end of http://code.activestate.com/recipes/438123/ }}}

Here is the website that I got it from: http://code.activestate.com/recipes/438123-file-tkinter-dialogs/

like image 178
udpatil Avatar answered Apr 08 '23 22:04

udpatil