1 # -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
5 # Tix Demostration Program
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
13 # This file demonstrates how to use the TixTree widget to display
14 # dynamic hierachical data (the files in the Unix file system)
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)
25 # The / directory is added in the "open" mode. The user can open it
26 # and then browse its subdirectories ...
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)
35 def adddir(tree
, dir):
39 text
= os
.path
.basename(dir)
40 tree
.hlist
.add(dir, itemtype
=Tix
.IMAGETEXT
, text
=text
,
41 image
=tree
.tk
.call('tix', 'getimage', 'folder'))
44 tree
.setmode(dir, 'open')
46 # No read permission ?
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.
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
57 def opendir(tree
, dir):
58 entries
= tree
.hlist
.info_children(dir)
60 # We have already loaded this directory. Let's just
61 # show all the child entries
63 # Note: since we load the directory only once, it will not be
64 # refreshed if the you add or remove files from this
68 tree
.hlist
.show_entry(entry
)
69 files
= os
.listdir(dir)
71 if os
.path
.isdir(dir + '/' + file):
72 adddir(tree
, dir + '/' + file)
74 tree
.hlist
.add(dir + '/' + file, itemtype
=Tix
.IMAGETEXT
, text
=file,
75 image
=tree
.tk
.call('tix', 'getimage', 'file'))
77 if __name__
== '__main__':