4

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.

3 Answers 3

8

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/

Sign up to request clarification or add additional context in comments.

Comments

0

Have a look at this (untested): http://www.java2s.com/Code/Python/GUI-Tk/SimpleEditor.htm You may just need to add the 'open' dialog but using the Tkinter documentation that should be easy.

Comments

0

I suggest that you do not use tkinter but that you use wxwindows. I have used both before with different level of success (I was just messing around with the basics). If you do decide to use wxwindows here is a website that is really useful: http://www.wxpython.org/onlinedocs.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.