Mercurial > cpython-lru_cache-weakref
annotate Demo/tix/samples/Tree.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | c3a096bfbd6b |
| children | c25e5b620a4c |
| rev | line source |
|---|---|
|
22345
1f73251c625a
Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents:
17098
diff
changeset
|
1 # -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*- |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
2 # |
|
17098
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
3 # $Id$ |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
4 # |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
5 # Tix Demostration Program |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
6 # |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
7 # This sample program is structured in such a way so that it can be |
|
22345
1f73251c625a
Patch #485959: Various changes to Tix demos.
Martin v. Löwis <martin@v.loewis.de>
parents:
17098
diff
changeset
|
8 # executed from the Tix demo program "tixwidgets.py": it must have a |
|
17098
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
9 # procedure called "RunSample". It should also have the "if" statment |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
10 # at the end of this file so that it can be run as a standalone |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
11 # program. |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
12 |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
13 # This file demonstrates how to use the TixTree widget to display |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
14 # dynamic hierachical data (the files in the Unix file system) |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
15 # |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
16 |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
17 import Tix, os |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
18 |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
19 def RunSample(w): |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
20 top = Tix.Frame(w, relief=Tix.RAISED, bd=1) |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
21 tree = Tix.Tree(top, options='separator "/"') |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
22 tree.pack(expand=1, fill=Tix.BOTH, padx=10, pady=10, side=Tix.LEFT) |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
23 tree['opencmd'] = lambda dir=None, w=tree: opendir(w, dir) |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
24 |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
25 # The / directory is added in the "open" mode. The user can open it |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
26 # and then browse its subdirectories ... |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
27 adddir(tree, "/") |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
28 |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
29 box = Tix.ButtonBox(w, orientation=Tix.HORIZONTAL) |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
30 box.add('ok', text='Ok', underline=0, command=w.destroy, width=6) |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
31 box.add('cancel', text='Cancel', underline=0, command=w.destroy, width=6) |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
32 box.pack(side=Tix.BOTTOM, fill=Tix.X) |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
33 top.pack(side=Tix.TOP, fill=Tix.BOTH, expand=1) |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
34 |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
35 def adddir(tree, dir): |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
36 if dir == '/': |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
37 text = '/' |
|
17098
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
38 else: |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
39 text = os.path.basename(dir) |
|
17098
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
40 tree.hlist.add(dir, itemtype=Tix.IMAGETEXT, text=text, |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
41 image=tree.tk.call('tix', 'getimage', 'folder')) |
|
17098
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
42 try: |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
43 os.listdir(dir) |
|
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
44 tree.setmode(dir, 'open') |
|
17098
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
45 except os.error: |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
46 # No read permission ? |
|
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
47 pass |
|
17098
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
48 |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
49 # This function is called whenever the user presses the (+) indicator or |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
50 # double clicks on a directory whose mode is "open". It loads the files |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
51 # inside that directory into the Tree widget. |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
52 # |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
53 # Note we didn't specify the closecmd option for the Tree widget, so it |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
54 # performs the default action when the user presses the (-) indicator or |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
55 # double clicks on a directory whose mode is "close": hide all of its child |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
56 # entries |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
57 def opendir(tree, dir): |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
58 entries = tree.hlist.info_children(dir) |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
59 if entries: |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
60 # We have already loaded this directory. Let's just |
|
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
61 # show all the child entries |
|
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
62 # |
|
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
63 # Note: since we load the directory only once, it will not be |
|
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
64 # refreshed if the you add or remove files from this |
|
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
65 # directory. |
|
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
66 # |
|
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
67 for entry in entries: |
|
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
68 tree.hlist.show_entry(entry) |
|
17098
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
69 files = os.listdir(dir) |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
70 for file in files: |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
71 if os.path.isdir(dir + '/' + file): |
|
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
72 adddir(tree, dir + '/' + file) |
|
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
73 else: |
|
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
74 tree.hlist.add(dir + '/' + file, itemtype=Tix.IMAGETEXT, text=file, |
|
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
22345
diff
changeset
|
75 image=tree.tk.call('tix', 'getimage', 'file')) |
|
17098
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
76 |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
77 if __name__ == '__main__': |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
78 root = Tix.Tk() |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
79 RunSample(root) |
|
989fed28b74f
Patch #410231: Add the Python Tix library.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
80 root.mainloop() |
