[Python-checkins] r63202 - in sandbox/trunk/ttk-gsoc: Doc/library/ttk.rst samples/dirbrowser.py

guilherme.polo python-checkins at python.org
Tue May 13 14:47:32 CEST 2008


Author: guilherme.polo
Date: Tue May 13 14:47:31 2008
New Revision: 63202

Log:
Improved dirbrowser sample a bit, you can move inside directories now.

Documented some options in ttk.rst which were marked with XXX.


Modified:
   sandbox/trunk/ttk-gsoc/Doc/library/ttk.rst
   sandbox/trunk/ttk-gsoc/samples/dirbrowser.py

Modified: sandbox/trunk/ttk-gsoc/Doc/library/ttk.rst
==============================================================================
--- sandbox/trunk/ttk-gsoc/Doc/library/ttk.rst	(original)
+++ sandbox/trunk/ttk-gsoc/Doc/library/ttk.rst	Tue May 13 14:47:31 2008
@@ -285,14 +285,18 @@
    +-----------------+--------------------------------------------------------+
    | option          | description                                            |
    +=================+========================================================+
-   | exportselection | XXX                                                    |
+   | exportselection | Boolean value. If set, the widget selection is linked  |
+   |                 | to the Window Manager selection (which can be returned |
+   |                 | by invoking Misc.selection_get, for example).          |
    +-----------------+--------------------------------------------------------+
    | justify         | Specifies how the text is aligned within the widget.   |
    |                 | One of "left", "center", or "right".                   |
    +-----------------+--------------------------------------------------------+
    | height          | Specifies the height of the pop-down listbox, in rows. |
    +-----------------+--------------------------------------------------------+
-   | postcommand     | XXX                                                    |
+   | postcommand     | A script (possibly registered with Misc.register) that |
+   |                 | is called immediately before displaying the values. It |
+   |                 | may specify which values to display.                   |
    +-----------------+--------------------------------------------------------+
    | state           | One of "normal", "readonly", or "disabled". In the     |
    |                 | "readonly" state, the value may not be edited directly,|
@@ -405,7 +409,7 @@
    +-----------+--------------------------------------------------------------+
    | compound  | Specifies how to display the image relative to the text, in  |
    |           | the case both options text and image are present. See        |
-   |           | label(n) for legal values. XXX                               |
+   |           | `Label Options`_ for legal values.                           |
    +-----------+--------------------------------------------------------------+
    | underline | Specifies the index (0-based) of a character to underline in |
    |           | the text string. The underlined character is used for        |

Modified: sandbox/trunk/ttk-gsoc/samples/dirbrowser.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/samples/dirbrowser.py	(original)
+++ sandbox/trunk/ttk-gsoc/samples/dirbrowser.py	Tue May 13 14:47:31 2008
@@ -3,9 +3,9 @@
 Based on an example found at:
 http://bitwalk.blogspot.com/2008/01/ttktreeview.html
 """
-
 import os
 import sys
+import glob
 import Tkinter
 
 if sys.version_info[0] > 2:
@@ -20,24 +20,29 @@
     path = tree.set(node, "fullpath")
     tree.delete(tree.get_children(node))
 
-    for p in os.listdir(path):
+    parent = tree.parent(node)
+    special_dirs = [] if parent else glob.glob('.') + glob.glob('..')
+   
+    for p in special_dirs + os.listdir(path):
         p = os.path.join(path, p)
         if os.path.isdir(p):
             type = "directory"
         elif os.path.isfile(p):
             type = "file"
 
-        id = tree.insert(node, "end", text=os.path.split(p)[1], 
-            values=[p, type])
+        fname = os.path.split(p)[1]
+        id = tree.insert(node, "end", text=fname, values=[p, type])
 
         if type == 'directory':
-            tree.insert(id, 0, text="dummy")
-            tree.item(id, text=os.path.split(p)[1])
+            if fname not in ('.', '..'):
+                tree.insert(id, 0, text="dummy")
+                tree.item(id, text=fname)
         elif type == 'file':
             size = os.stat(p).st_size
             tree.set(id, "size", "%d bytes" % size)
 
-def popuplate_roots(tree):
+
+def populate_roots(tree):
     dir = os.path.abspath('.')
     node = tree.insert('', 'end', text=dir, values=[dir, "directory"])
     populate_tree(tree, node)
@@ -46,6 +51,20 @@
     tree = event.widget
     populate_tree(tree, tree.focus())
 
+def change_dir(event):
+    tree = event.widget
+    node = tree.focus()
+    if tree.parent(node):
+        path = os.path.abspath(tree.set(node, "fullpath"))
+        if os.path.isdir(path):
+            try:
+                os.chdir(path)
+            except OSError, e:
+                print e
+            else:
+                tree.delete(tree.get_children(''))
+                populate_roots(tree)
+
 root = Tkinter.Tk()
 
 vsb = ttk.Scrollbar(orient="vertical")
@@ -57,12 +76,13 @@
 vsb['command'] = tree.yview
 hsb['command'] = tree.xview
 
-tree.heading("#0", text="Directory Structure")
-tree.heading("size", text="File Size")
-tree.column("size", stretch=0, width=70)
+tree.heading("#0", text="Directory Structure", anchor='w')
+tree.heading("size", text="File Size", anchor='w')
+tree.column("size", stretch=0, width=100)
 
-popuplate_roots(tree)
+populate_roots(tree)
 tree.bind('<<TreeviewOpen>>', update_tree)
+tree.bind('<Double-Button-1>', change_dir)
 
 # Arrange the tree and its scrollbars in the toplevel
 tree.grid(column=0, row=0, sticky='nswe')


More information about the Python-checkins mailing list