faqts : Computers : Programming : Languages : Python : Tkinter

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

10 of 18 people (56%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

I'm writing a script that will act as a GUI interface to another script, and I want to have file browsing ability. How do I call this in Tkinter?

Jul 16th, 2000 17:50
unknown unknown, John Grayson


This is not really a Tkinter issue, since you can fire up a file browser 
window from a simple Python program. However, it may introduce some 
special problems if you're doing it from a GUI. Therefore, here is some 
sample code to illustrate how to do this for Windows and Solaris 
platforms. You would have to adjust for other platforms. Note that if 
you're doing this just for Unix, that you don't need to thread: you can 
just fire off the command in the background...
from Tkinter import *
from tkSimpleDialog import *
import sys, os, thread
class App:
    def __init__(self, master):
        self.master = master
        Label(master, text='Browsing:').pack(side=LEFT,
                                             padx=4, pady=15)
        self.entry  = StringVar()
        Entry(master, width=50, bg='gray', textvariable=self.entry,
              state=DISABLED).pack(side=LEFT, padx=15, pady=15)
        Button(master, text='Browse...',
               command=self.browse).pack(side=BOTTOM)
    def browse(self):
        path  = askstring("Browse", "Enter directory")
        self.entry.set(path)
        browser = Browse(folder=path)
class Browse:
    def __init__(self, folder='.'):
        if sys.platform == 'win32':
            cmd = 'Explorer %s' % folder
        else:
            cmd = 'dtfile -folder %s' % folder
        thread.start_new_thread(self.doIt, (cmd,))
    def doIt(self, where):
        os.system(where)
root = Tk()
display = App(root)
root.mainloop()
Hope this helps to get you started.
BTW: Chapter 18 of Python and Tkinter Programming covers
handling asynchronous events and things that block the
mainloop...