PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Demo/tix/samples/Tree.py

http://unladen-swallow.googlecode.com/
Python | 80 lines | 40 code | 8 blank | 32 comment | 8 complexity | 33b35ea1af91441dde260e8d66862455 MD5 | raw file
 1# -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
 2#
 3# $Id: Tree.py 36560 2004-07-18 06:16:08Z tim_one $
 4#
 5# Tix Demostration Program
 6#
 7# This sample program is structured in such a way so that it can be
 8# executed from the Tix demo program "tixwidgets.py": it must have a
 9# procedure called "RunSample". It should also have the "if" statment
10# at the end of this file so that it can be run as a standalone
11# program.
12
13# This file demonstrates how to use the TixTree widget to display
14# dynamic hierachical data (the files in the Unix file system)
15#
16
17import Tix, os
18
19def RunSample(w):
20    top = Tix.Frame(w, relief=Tix.RAISED, bd=1)
21    tree = Tix.Tree(top, options='separator "/"')
22    tree.pack(expand=1, fill=Tix.BOTH, padx=10, pady=10, side=Tix.LEFT)
23    tree['opencmd'] = lambda dir=None, w=tree: opendir(w, dir)
24
25    # The / directory is added in the "open" mode. The user can open it
26    # and then browse its subdirectories ...
27    adddir(tree, "/")
28
29    box = Tix.ButtonBox(w, orientation=Tix.HORIZONTAL)
30    box.add('ok', text='Ok', underline=0, command=w.destroy, width=6)
31    box.add('cancel', text='Cancel', underline=0, command=w.destroy, width=6)
32    box.pack(side=Tix.BOTTOM, fill=Tix.X)
33    top.pack(side=Tix.TOP, fill=Tix.BOTH, expand=1)
34
35def adddir(tree, dir):
36    if dir == '/':
37        text = '/'
38    else:
39        text = os.path.basename(dir)
40    tree.hlist.add(dir, itemtype=Tix.IMAGETEXT, text=text,
41                   image=tree.tk.call('tix', 'getimage', 'folder'))
42    try:
43        os.listdir(dir)
44        tree.setmode(dir, 'open')
45    except os.error:
46        # No read permission ?
47        pass
48
49# This function is called whenever the user presses the (+) indicator or
50# double clicks on a directory whose mode is "open". It loads the files
51# inside that directory into the Tree widget.
52#
53# Note we didn't specify the closecmd option for the Tree widget, so it
54# performs the default action when the user presses the (-) indicator or
55# double clicks on a directory whose mode is "close": hide all of its child
56# entries
57def opendir(tree, dir):
58    entries = tree.hlist.info_children(dir)
59    if entries:
60        # We have already loaded this directory. Let's just
61        # show all the child entries
62        #
63        # Note: since we load the directory only once, it will not be
64        #       refreshed if the you add or remove files from this
65        #       directory.
66        #
67        for entry in entries:
68            tree.hlist.show_entry(entry)
69    files = os.listdir(dir)
70    for file in files:
71        if os.path.isdir(dir + '/' + file):
72            adddir(tree, dir + '/' + file)
73        else:
74            tree.hlist.add(dir + '/' + file, itemtype=Tix.IMAGETEXT, text=file,
75                           image=tree.tk.call('tix', 'getimage', 'file'))
76
77if __name__ == '__main__':
78    root = Tix.Tk()
79    RunSample(root)
80    root.mainloop()