blob: e46ff60b238aa51910489ce2aacc95c3f91a7bae [file] [log] [blame]
Martin v. Löwis8ec03e02002-03-17 18:19:13 +00001# -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
Tim Peters182b5ac2004-07-18 06:16:08 +00002#
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +00003# $Id$
4#
5# Tix Demostration Program
6#
7# This sample program is structured in such a way so that it can be
Martin v. Löwis8ec03e02002-03-17 18:19:13 +00008# executed from the Tix demo program "tixwidgets.py": it must have a
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +00009# 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
Benjamin Petersond6d63f52009-01-04 18:53:28 +000017import tkinter.tix, os
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000018
19def RunSample(w):
Benjamin Petersond6d63f52009-01-04 18:53:28 +000020 top = tkinter.tix.Frame(w, relief=tkinter.tix.RAISED, bd=1)
21 tree = tkinter.tix.Tree(top, options='separator "/"')
22 tree.pack(expand=1, fill=tkinter.tix.BOTH, padx=10, pady=10, side=tkinter.tix.LEFT)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000023 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
Benjamin Petersond6d63f52009-01-04 18:53:28 +000029 box = tkinter.tix.ButtonBox(w, orientation=tkinter.tix.HORIZONTAL)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000030 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)
Benjamin Petersond6d63f52009-01-04 18:53:28 +000032 box.pack(side=tkinter.tix.BOTTOM, fill=tkinter.tix.X)
33 top.pack(side=tkinter.tix.TOP, fill=tkinter.tix.BOTH, expand=1)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000034
35def adddir(tree, dir):
36 if dir == '/':
Tim Peters182b5ac2004-07-18 06:16:08 +000037 text = '/'
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000038 else:
Tim Peters182b5ac2004-07-18 06:16:08 +000039 text = os.path.basename(dir)
Benjamin Petersond6d63f52009-01-04 18:53:28 +000040 tree.hlist.add(dir, itemtype=tkinter.tix.IMAGETEXT, text=text,
Tim Peters182b5ac2004-07-18 06:16:08 +000041 image=tree.tk.call('tix', 'getimage', 'folder'))
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000042 try:
Tim Peters182b5ac2004-07-18 06:16:08 +000043 os.listdir(dir)
44 tree.setmode(dir, 'open')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000045 except os.error:
Tim Peters182b5ac2004-07-18 06:16:08 +000046 # No read permission ?
47 pass
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000048
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:
Tim Peters182b5ac2004-07-18 06:16:08 +000060 # 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)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000069 files = os.listdir(dir)
70 for file in files:
Tim Peters182b5ac2004-07-18 06:16:08 +000071 if os.path.isdir(dir + '/' + file):
72 adddir(tree, dir + '/' + file)
73 else:
Benjamin Petersond6d63f52009-01-04 18:53:28 +000074 tree.hlist.add(dir + '/' + file, itemtype=tkinter.tix.IMAGETEXT, text=file,
Tim Peters182b5ac2004-07-18 06:16:08 +000075 image=tree.tk.call('tix', 'getimage', 'file'))
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000076
77if __name__ == '__main__':
Benjamin Petersond6d63f52009-01-04 18:53:28 +000078 root = tkinter.tix.Tk()
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000079 RunSample(root)
80 root.mainloop()