Martin v. Löwis | 8ec03e0 | 2002-03-17 18:19:13 +0000 | [diff] [blame] | 1 | # -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*- |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame^] | 2 | # |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 3 | # $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öwis | 8ec03e0 | 2002-03-17 18:19:13 +0000 | [diff] [blame] | 8 | # executed from the Tix demo program "tixwidgets.py": it must have a |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 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 | |
| 17 | import Tix, os |
| 18 | |
| 19 | def 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 | |
| 35 | def adddir(tree, dir): |
| 36 | if dir == '/': |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame^] | 37 | text = '/' |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 38 | else: |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame^] | 39 | text = os.path.basename(dir) |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 40 | tree.hlist.add(dir, itemtype=Tix.IMAGETEXT, text=text, |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame^] | 41 | image=tree.tk.call('tix', 'getimage', 'folder')) |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 42 | try: |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame^] | 43 | os.listdir(dir) |
| 44 | tree.setmode(dir, 'open') |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 45 | except os.error: |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame^] | 46 | # No read permission ? |
| 47 | pass |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 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 |
| 57 | def opendir(tree, dir): |
| 58 | entries = tree.hlist.info_children(dir) |
| 59 | if entries: |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame^] | 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) |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 69 | files = os.listdir(dir) |
| 70 | for file in files: |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame^] | 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')) |
Martin v. Löwis | b21cb5f | 2001-03-21 07:42:07 +0000 | [diff] [blame] | 76 | |
| 77 | if __name__ == '__main__': |
| 78 | root = Tix.Tk() |
| 79 | RunSample(root) |
| 80 | root.mainloop() |