Tkinter?

John Grayson johngrayson at home.com
Sat Jul 15 14:17:36 EDT 2000


In article <Ep0c5.24126$6y5.18334880 at news2.rdc2.tx.home.com>,
  "David L. Heald Jr." <blinocac1 at home.com> wrote:
> 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?
I have
> looked through 2 different books, and a couple of help files and have
yet to
> find anything.
>
>

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...

    John Grayson


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list