KevinWalzer: PyLocate is a simple GUI wrapper for the Unix "locate" command. I wrote it to illustrate how TableListWrapper can be used in a Tkinter application. To use this, you need TableListWrapper installed. You also must have TableList installed with Tcl/Tk.

###pylocate.py: copyright 2006 by Kevin Walzer, kw@codebykevin.com.
###My code is freely reusable under MIT license.
###Images are from Nuvola icon set by David Vignoni, licensed under LGPL.

"""
The purpose of this application is to illustrate how to integrate some recent advances in
Tk development into a Tkinter application. My focus here is the Tablelist widget, developed
by Csaba Nemethi and initially wrapped for Tkinter by Martin Franklin, with additional
modifications by Kevin Walzer; and , in the pylocate-tile version, the Tile widget set, developed
by Joe English and others, initially wrapped for Tkinter by Martin Franklin, and additionally modified
by Kevin Walzer.

To run pylocate or pylocate-tile, you will need TableList.py and/or Tile.py and Tablelist_Tile.py
installed in your Python site-packages directory; and Tablelist and/or Tile themselves will
have to be installed somewhere on your Tcl/Tk auto_path. Tablelist is a pure-Tcl package,
while Tile is a binary extension that must be built from source. Both can be found for numerous
platforms in the ActiveTcl distribution.

Additionally, because pylocate wraps the Unix "locate" command-line program with a GUI, you will
need to have locate installed on your system. This program is a standard part of most Linux and Unix
systems, including Mac OS X, and Windows ports are also available for separate download on the Internet.

This application is coded without classes. Coming from a Tcl background, I am more comfortable with
a procedural approach, and some of the way this application is structured (using global variables
extensively) may not be totally "Pythonic." Still, the example should serve well enough for
most purposes. --Kevin Walzer, 2006
"""


from Tkinter import *
from tkMessageBox import *
import os
import sys
from TableList import *


searchterm = ""
table = ""
root = ""
log = ""
textimage = ""

def pylocate():

    """
    The pylocate function takes a search term and passes that term on to the standard Unix "locate"
    command via an os.popen call.The system call returns a list of files that match the search term,
    as defined in the "locate" database. Finally, the pylocate parses the list into a tuple of file name
    and file directories, and displays each line in the Tablelist widget, while configuring each line of
    the widget with an icon representing a file.
    """

    global searchterm
    global root
    global log
    global textimage


    textimage= PhotoImage(data="""
    R0lGODlhEAAQANUAAAAAAODg4MjIyIiIiODg6OjQ+NjY4ODo6ODQ8OjY8OjQ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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAdGV4dAAAAABDb3B5cmln
    aHQgQXBwbGUgQ29tcHV0ZXIsIEluYy4sIDIwMDUAAAAAACwAAAAAEAAQAAAGmUCNZEgsDgWDFmjF
    Yq2e0Cjk8VkVFKRS6XQiEBwhVYS6SiRMpDQJdfCmBGQzOuv1OlTwKgKRLdW/YXAcZQpzKH8UeA8Y
    KwsLJH9fBgYkEouNC211Dg6TlZcTmZuTnpaMExOaBBSkBiimK6iaAaykrw8bKw0NBK0GFiIiByCX
    DAxpqpIqlhoBI8/QFRDTEAEdHi0Z2tvc2xctQQA7
    """)

    localvar=searchterm.get()

    print localvar

    if localvar == "":
        showerror("Error", "Please enter a search term")

    log.delete("0", "end")

    file = os.popen("locate %s" % localvar, "r")

    for item in file: ##parse list
        filename = os.path.basename(item) ###get filename
        finalfile = filename.strip("\n") ###strip newline from filename
        dirname = os.path.dirname(item) ###get directory name
        filelist = (finalfile, dirname) ###merge into tuple
        log.insert("end", filelist) ###dump into tablelist widget
        log.cellconfigure("end,0", image=textimage) #add file graphic to each line


    file.close()


def drawGUI():

    """
    The drawGUI function lays out the basic graphical interface for PyLocate.
    In the pylocate-tile version, it makes extensive use of the Tile widget set.
    In the pylocate version, standard Tk widgets are used.

    The function makes some accommodations to platform-specific conventions
    on Mac OS X on menu items (the Apple application menu) and scrollbars (Tile scrollbars
    do not work correctly on OS X) via the "tk windowingsystem" call. These are to work around
    bugs in the Tile set and/or Mac-specific peculiarities that are not found on Windows or X11 systems.

    """


    global searchterm
    global root
    global log
    global textimage

    root = Tk()
    root.title("PyLocate")

    searchterm = StringVar()
    mb=Menu(root)
    root.configure(menu=mb)

    if root.tk.call("tk", "windowingsystem") == "aqua":   ###add apple menu, "About" item
        applemenu=Menu(mb, name="apple")
        mb.add_cascade(label="PyLocate", menu=applemenu)
        applemenu.add_command(label="About PyLocate", command=aboutprog)


    locatemenu=Menu(mb, tearoff=0)
    mb.add_cascade(label="Locate", menu=locatemenu)
    locatemenu.add_command(label="Locate Search Term", command=pylocate)
    if not root.tk.call("tk", "windowingsystem") == "aqua": ##quit menu
        locatemenu.add_command(label="Exit", command=root.quit)


    helpmenu = Menu(mb, name="help", tearoff=0)
    mb.add_cascade(label="Help", menu=helpmenu)
    helpmenu.add_command(label="PyLocate Help", command=runhelp)
    if not root.tk.call("tk", "windowingsystem") == "aqua":  ###about menu goes in "help menu" on non-Mac platforms
        helpmenu.add_command(label="About", command=aboutprog)

    mainframe = Frame(root)
    mainframe.pack(side=TOP, fill=BOTH, expand=YES)

    topframe=Frame(mainframe)
    topframe.pack(side=TOP, fill=BOTH, expand=NO)

    bottomlabel=Label(mainframe, text="", width = 80)
    bottomlabel.pack(side=BOTTOM, fill=BOTH, expand=NO)

    bottomframe=Frame(mainframe)
    bottomframe.pack(side=BOTTOM, fill=BOTH, expand=YES)

    searchglass= PhotoImage(data="""
    R0lGODlhFgAWAOYAAAAAAAgAAMjY6HiQuMjg+LjY+Ljg+LDY+JA4GPjgUKioqNDo+MDg+MDY+PjY
    UMjY8PjgWNjg4GBYWJDI+PiQEKDQ+MDIyLC4wICo4Mjo+LCwuKiw2Iig2CgoKMgoIHh4eJCQkHiY
    0HCY0PjoeJjI+JiwwJiosJCYsLBAEJCw8JCg2IiIiBgYGHBwcGBgYIjA+EBAQKB4eMhYQICAgHiA
    iIiIkICAeIiAeIiIgDg4OGhoYFhQUGhoaKg4GPioMOjQcNhIAPiQGFgQCHiAkJiQkJiYkPjQOJiY
    mKCQkKjQ+MjQ2MDAwLi4wBAQEDAwMDAoKFBQUFhYWHAQALDAwJjQ+Ji48KCosIiYyJC4+LAwALDQ
    8LDI+MjQ6KjY+LDQ+IiYoMDI4NBIAPjIOOBIAPiYGJiYoBgQEGgYCNDg+Jg4GKDA2HiIkNjo+Njw
    +ODw+JjA8KDA+AAAAAAAAAA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AAAFgAWAAAH8IBxgoOEhYaHN0VHOBKHjjZMGyocVycrHY6ERGApW2hsDVUhZU6ZcR9cWAwLrBkZ
    SSIrTZlLcEkFBQwMBQcHEwM7ji1cLxVJvQddSSRUGB+OIA0TJCQV1tQTFW9DZodFDUkV1ONUt15r
    LIc4Dwa8ykldBwYZWjSzhjpKDBm6/QQZbdTwyKSAAKsFGQ6yyfAlRyYXFgi0YUORjZs2JWYEMNWC
    iYAFbS4KMLHCQgwPplyA0DDlgpUaUZ4giTAChak4OaDsgDFIxg8jCXrcPDSGghgIaYYaAmI0AQKl
    hYAEEePgDFRCYcj4EHKVUBYpXR0FAgA7
    """)

    exitbutton = PhotoImage(data="""
    R0lGODlhFgAWAOYAAAAAAJAAAHAICJBAQJAICHggINjAwJAgIHgQEKgwMKA4OLA4OLg4OMg4OMhA
    OMigoOBQOOjo4NC4uNhYSODg4OBgWNhoWODQ0PhwWPhwYODIyMgAAMhIQJgICMgwKMA4ONigoOhQ
    OPjo6Ni4uPDg2PBYOPjQyODI0HgICHAQEMiwsPDw8PDw6PDo6PDY2NioqOjY2OjQ0KAICIhISNCg
    oNhQSKgQENiwsKgQGNgQANCwuOC4uMhISOC4wOi4sOi4uMAAAKgAAKAAAKhAQIAQELAoKJg4OMgY
    AKAQEMg4MMggEMAIALgAALAIAPjw8Ojg4OjI0PD4+JgYGLg4MLgQANAIAOBQQMgQAPhAGOAQAIAY
    GMAQAOAgAOgYANAQAPAQAPjw6KBISIgYGOgoAOCoqJgAAOCosPDg4PAwAOjo6JgQENggAPj48PDg
    6MgIANAYAPj4+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5
    BAEAAHEAIf/8SUNDUkdCRzEwMTIAAALMYXBwbAIAAABtbnRyUkdCIFhZWiAH1gALABIAAAAAAABh
    Y3NwQVBQTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA9tYAAQAAAADTLWFwcGwAAAAAAAAAAA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LlcjLhyhF5DlpIQJEI
    1bdwS7Yw+aARwSGPzuQlccCESpApaQwIKKSJYjgPT1h8CNJEyIInKlIQSgDHwgaAD3W0mCIkSJkh
    UQYQkgLjhxIgHkhoIIJCQoQFZTo8oFCgkIIVPni4uEAQhYEnQyEeRJlxyAgMOCMICkqhIgoFuojE
    hIlISMCAsooSK14sKBAAOw==
    """)


    searchbutton=Button(topframe, image=searchglass, command=pylocate, text="Search")
    searchbutton.image=searchglass
    searchbutton.configure(compound=TOP)
    searchbutton.pack(side=LEFT,  expand = NO)

    quitbutton= Button(topframe, image=exitbutton, command=root.quit, text="Exit")
    quitbutton.image=exitbutton
    quitbutton.configure(compound=TOP)
    quitbutton.pack(side=LEFT, expand=NO)

    entryterm=Entry(topframe, textvariable=searchterm, bg="white")
    entryterm.pack(side=RIGHT)
    entryterm.bind("<Return>", (lambda event: pylocate()))


    entrylabel=Label(topframe, text="Search:")
    entrylabel.pack(side=RIGHT)

    scrollbar = Scrollbar(bottomframe)
    scrollbar.pack(fill=Y, expand=NO, side=RIGHT)

    table=TableList(bottomframe,
                    background="white",
                    columns=(0, "File", 0, "Path"),
                    stretch="all",
                    )
    table.pack(fill=BOTH, expand=YES, side=LEFT)


    scrollbar.configure(command=table.yview)
    table.configure(yscrollcommand=scrollbar.set)
    log = table

def aboutprog():

    showinfo("About PyLocate", "PyLocate: A GUI for locate")

def runhelp():

    showinfo("PyLocate Help", "No help available at this time")


if __name__ == "__main__":
  drawGUI()
  mainloop()

Screenshot under OS X (Aqua):

http://www.codebykevin.com/pylocate-aqua.png

Screenshot under X11:

http://www.codebykevin.com/pylocate-x11.png

For an example of a similar application that uses the Tile widget set with platform-native theming, see PyLocateTile.

tkinter: PyLocate (last edited 2010-07-26 11:59:12 by localhost)