[Python-checkins] r65974 - in sandbox/trunk/ttk-gsoc: Doc/library/ttk.rst src/2.x/test/test_treeview.py src/2.x/ttk.py src/3.x/test/test_treeview.py src/3.x/ttk.py

guilherme.polo python-checkins at python.org
Fri Aug 22 18:18:02 CEST 2008


Author: guilherme.polo
Date: Fri Aug 22 18:18:01 2008
New Revision: 65974

Log:
Some minor modifications in the Treeview class;
Started tests for Treeview (a lot is missing here);
Updated documentation for the column method of Treeview;
Some minor updates in Treeview documentation to match current API.


Added:
   sandbox/trunk/ttk-gsoc/src/2.x/test/test_treeview.py
   sandbox/trunk/ttk-gsoc/src/3.x/test/test_treeview.py
Modified:
   sandbox/trunk/ttk-gsoc/Doc/library/ttk.rst
   sandbox/trunk/ttk-gsoc/src/2.x/ttk.py
   sandbox/trunk/ttk-gsoc/src/3.x/ttk.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	Fri Aug 22 18:18:01 2008
@@ -850,15 +850,17 @@
 
       Returns the list of children belonging to *item*.
 
-      If *item* is not specified, returns all root children.
+      If *item* is not specified, returns root children.
 
 
-   .. method:: set_children(item, newchildren)
+   .. method:: set_children(item, *newchildren)
 
       Replaces item's child with *newchildren*.
 
       Children present in item that are not present in *newchildren* are
-      detached from tree. No items in *newchildren* may be an ancestor of item.
+      detached from tree. No items in *newchildren* may be an ancestor of
+      item. Note that not specifying *newchildren* results in detaching
+      *item*'s children.
 
 
    .. method:: column(column, **kw)
@@ -869,6 +871,24 @@
       If an option is specified with value None, returns the value of that
       option. Otherwise, the options are updated with the specified values.
 
+      The valid options/values are:
+
+      * id
+         Returns the column name, this is a read-only option.
+      * anchor: One of the standard Tk anchor values.
+         Specifies how the text in this column should be aligned with respect
+         to the cell.
+      * minwidth: width
+         The minimum width of the column in pixels. The treeview widget will
+         not make the column any smaller than the specified by this option when
+         the widget is resized or the user drags a column.
+      * stretch: True/False
+         Specifies wheter or not the column's width should be adjusted when
+         the widget is resized.
+      * width: width
+         The width of the column in pixels.
+
+      To configure the tree column, call this with column = "#0"
 
    .. method:: delete(items)
 
@@ -877,7 +897,7 @@
       The root item may not be deleted.
 
 
-   .. method:: detach(items)
+   .. method:: detach(*items)
 
       Unlinks all of the specified *items* from the tree.
 
@@ -902,7 +922,7 @@
 
       Query or modify the heading options for the specified *column*.
 
-      Valid options/values are:
+      The valid options/values are:
 
       * text: text
          The text to display in the column heading.
@@ -914,7 +934,7 @@
       * command: callback
          A callback to be invoked when the heading label is pressed.
 
-       To configure the tree column heading, call this with column = "#0"
+      To configure the tree column heading, call this with column = "#0"
 
 
    .. method:: identify(component, x, y)

Added: sandbox/trunk/ttk-gsoc/src/2.x/test/test_treeview.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/ttk-gsoc/src/2.x/test/test_treeview.py	Fri Aug 22 18:18:01 2008
@@ -0,0 +1,152 @@
+import unittest
+import Tkinter
+import ttk
+
+import support
+
+class TreeviewTest(unittest.TestCase):
+
+    def setUp(self):
+        self.tv = ttk.Treeview()
+
+    def tearDown(self):
+        self.tv.destroy()
+
+
+    def test_bbox(self):
+        self.tv.pack()
+        self.failUnlessEqual(self.tv.bbox(''), '')
+        self.tv.wait_visibility()
+        self.tv.update()
+
+        item_id = self.tv.insert('', 'end')
+        children = self.tv.get_children()
+        self.failUnless(children)
+
+        bbox = self.tv.bbox(children[0])
+        self.failUnlessEqual(len(bbox), 4)
+        self.failUnless(isinstance(bbox, tuple))
+        for item in bbox:
+            if not isinstance(item, int):
+                self.fail("Invalid bounding box: %s" % bbox)
+                break
+
+        # compare width in bboxes
+        self.tv['columns'] = ['test']
+        self.tv.column('test', width=50)
+        bbox_column0 = self.tv.bbox(children[0], 0)
+        root_width = self.tv.column('#0', width=None)
+        self.failUnlessEqual(bbox_column0[0], bbox[0] + root_width)
+
+        # verify that bbox of a closed item is the empty string
+        child1 = self.tv.insert(item_id, 'end')
+        self.failUnlessEqual(self.tv.bbox(child1), '')
+
+
+    def test_children(self):
+        # no children yet, should get an empty tuple
+        self.failUnlessEqual(self.tv.get_children(), ())
+
+        item_id = self.tv.insert('', 'end')
+        self.failUnless(isinstance(self.tv.get_children(), tuple))
+        self.failUnlessEqual(self.tv.get_children()[0], item_id)
+
+        # add item_id and child3 as children of child2
+        child2 = self.tv.insert('', 'end')
+        child3 = self.tv.insert('', 'end')
+        self.tv.set_children(child2, item_id, child3)
+        self.failUnlessEqual(self.tv.get_children(child2), (item_id, child3))
+
+        # child3 has child2 as parent, thus trying to set child2 as a children
+        # of child3 should result in an error
+        self.failUnlessRaises(Tkinter.TclError,
+            self.tv.set_children, child3, child2)
+
+        # remove child2 children
+        self.tv.set_children(child2)
+        self.failUnlessEqual(self.tv.get_children(child2), ())
+
+        # remove root's children
+        self.tv.set_children('')
+        self.failUnlessEqual(self.tv.get_children(), ())
+
+
+    def test_column(self):
+        # return a dict with all options/values
+        self.failUnless(isinstance(self.tv.column('#0'), dict))
+        # return a single value of the given option
+        self.failUnless(isinstance(self.tv.column('#0', width=None), int))
+        # set a new value for an option
+        self.tv.column('#0', width=10)
+        self.failUnlessEqual(self.tv.column('#0', width=None), 10)
+        # check read-only option
+        self.failUnlessRaises(Tkinter.TclError, self.tv.column, '#0', id='X')
+
+        self.failUnlessRaises(Tkinter.TclError, self.tv.column, 'invalid')
+        invalid_kws = [
+            {'unknown_option': 'some value'},  {'stretch': 'wrong'},
+            {'anchor': 'wrong'}, {'width': 'wrong'}, {'minwidth': 'wrong'}
+        ]
+        for kw in invalid_kws:
+            self.failUnlessRaises(Tkinter.TclError, self.tv.column, '#0',
+                **kw)
+
+
+    def test_delete(self):
+        self.failUnlessRaises(Tkinter.TclError, self.tv.delete, '#0')
+
+        item_id = self.tv.insert('', 'end')
+        item2 = self.tv.insert(item_id, 'end')
+        self.failUnlessEqual(self.tv.get_children(), (item_id, ))
+        self.failUnlessEqual(self.tv.get_children(item_id), (item2, ))
+
+        self.tv.delete(item_id)
+        self.failIf(self.tv.get_children())
+
+        # reattach should fail
+        self.failUnlessRaises(Tkinter.TclError,
+            self.tv.reattach, item_id, '', 'end')
+
+
+    def test_detach_reattach(self):
+        item_id = self.tv.insert('', 'end')
+        item2 = self.tv.insert(item_id, 'end')
+
+        self.failUnlessEqual(self.tv.get_children(), (item_id, ))
+        self.failUnlessEqual(self.tv.get_children(item_id), (item2, ))
+
+        # detach item with children
+        self.tv.detach(item_id)
+        self.failIf(self.tv.get_children())
+
+        # reattach item with children
+        self.tv.reattach(item_id, '', 'end')
+        self.failUnlessEqual(self.tv.get_children(), (item_id, ))
+        self.failUnlessEqual(self.tv.get_children(item_id), (item2, ))
+
+        # move a children to the root
+        self.tv.move(item2, '', 'end')
+        self.failUnlessEqual(self.tv.get_children(), (item_id, item2))
+        self.failUnlessEqual(self.tv.get_children(item_id), ())
+
+        # bad values
+        self.failUnlessRaises(Tkinter.TclError,
+            self.tv.reattach, 'nonexistant', '', 'end')
+        self.failUnlessRaises(Tkinter.TclError,
+            self.tv.detach, 'nonexistant')
+        self.failUnlessRaises(Tkinter.TclError,
+            self.tv.reattach, item2, 'otherparent', 'end')
+        self.failUnlessRaises(Tkinter.TclError,
+            self.tv.reattach, item2, '', 'invalid')
+
+        # multiple detach
+        self.tv.detach(item_id, item2)
+        self.failUnlessEqual(self.tv.get_children(), ())
+        self.failUnlessEqual(self.tv.get_children(item_id), ())
+
+
+def test_main():
+    support.run(TreeviewTest)
+
+if __name__ == "__main__":
+    test_main()

Modified: sandbox/trunk/ttk-gsoc/src/2.x/ttk.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/2.x/ttk.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/2.x/ttk.py	Fri Aug 22 18:18:01 2008
@@ -1154,11 +1154,11 @@
     def get_children(self, item=None):
         """Returns a tuple of children belonging to item.
         
-        If item is not specified, returns all root children."""
-        return self.tk.call(self._w, "children", item or '')
+        If item is not specified, returns root children."""
+        return self.tk.call(self._w, "children", item or '') or ()
 
 
-    def set_children(self, item, newchildren):
+    def set_children(self, item, *newchildren):
         """Replaces item's child with newchildren.
 
         Children present in item that are not present in newchildren
@@ -1183,7 +1183,7 @@
         self.tk.call(self._w, "delete", items)
 
 
-    def detach(self, items):
+    def detach(self, *items):
         """Unlinks all of the specified items from the tree.
 
         The items and all of their descendants are still present, and may

Added: sandbox/trunk/ttk-gsoc/src/3.x/test/test_treeview.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/ttk-gsoc/src/3.x/test/test_treeview.py	Fri Aug 22 18:18:01 2008
@@ -0,0 +1,152 @@
+import unittest
+import tkinter
+import ttk
+
+import support
+
+class TreeviewTest(unittest.TestCase):
+
+    def setUp(self):
+        self.tv = ttk.Treeview()
+
+    def tearDown(self):
+        self.tv.destroy()
+
+
+    def test_bbox(self):
+        self.tv.pack()
+        self.failUnlessEqual(self.tv.bbox(''), '')
+        self.tv.wait_visibility()
+        self.tv.update()
+
+        item_id = self.tv.insert('', 'end')
+        children = self.tv.get_children()
+        self.failUnless(children)
+
+        bbox = self.tv.bbox(children[0])
+        self.failUnlessEqual(len(bbox), 4)
+        self.failUnless(isinstance(bbox, tuple))
+        for item in bbox:
+            if not isinstance(item, int):
+                self.fail("Invalid bounding box: %s" % bbox)
+                break
+
+        # compare width in bboxes
+        self.tv['columns'] = ['test']
+        self.tv.column('test', width=50)
+        bbox_column0 = self.tv.bbox(children[0], 0)
+        root_width = self.tv.column('#0', width=None)
+        self.failUnlessEqual(bbox_column0[0], bbox[0] + root_width)
+
+        # verify that bbox of a closed item is the empty string
+        child1 = self.tv.insert(item_id, 'end')
+        self.failUnlessEqual(self.tv.bbox(child1), '')
+
+
+    def test_children(self):
+        # no children yet, should get an empty tuple
+        self.failUnlessEqual(self.tv.get_children(), ())
+
+        item_id = self.tv.insert('', 'end')
+        self.failUnless(isinstance(self.tv.get_children(), tuple))
+        self.failUnlessEqual(self.tv.get_children()[0], item_id)
+
+        # add item_id and child3 as children of child2
+        child2 = self.tv.insert('', 'end')
+        child3 = self.tv.insert('', 'end')
+        self.tv.set_children(child2, item_id, child3)
+        self.failUnlessEqual(self.tv.get_children(child2), (item_id, child3))
+
+        # child3 has child2 as parent, thus trying to set child2 as a children
+        # of child3 should result in an error
+        self.failUnlessRaises(tkinter.TclError,
+            self.tv.set_children, child3, child2)
+
+        # remove child2 children
+        self.tv.set_children(child2)
+        self.failUnlessEqual(self.tv.get_children(child2), ())
+
+        # remove root's children
+        self.tv.set_children('')
+        self.failUnlessEqual(self.tv.get_children(), ())
+
+
+    def test_column(self):
+        # return a dict with all options/values
+        self.failUnless(isinstance(self.tv.column('#0'), dict))
+        # return a single value of the given option
+        self.failUnless(isinstance(self.tv.column('#0', width=None), int))
+        # set a new value for an option
+        self.tv.column('#0', width=10)
+        self.failUnlessEqual(self.tv.column('#0', width=None), 10)
+        # check read-only option
+        self.failUnlessRaises(tkinter.TclError, self.tv.column, '#0', id='X')
+
+        self.failUnlessRaises(tkinter.TclError, self.tv.column, 'invalid')
+        invalid_kws = [
+            {'unknown_option': 'some value'},  {'stretch': 'wrong'},
+            {'anchor': 'wrong'}, {'width': 'wrong'}, {'minwidth': 'wrong'}
+        ]
+        for kw in invalid_kws:
+            self.failUnlessRaises(tkinter.TclError, self.tv.column, '#0',
+                **kw)
+
+
+    def test_delete(self):
+        self.failUnlessRaises(tkinter.TclError, self.tv.delete, '#0')
+
+        item_id = self.tv.insert('', 'end')
+        item2 = self.tv.insert(item_id, 'end')
+        self.failUnlessEqual(self.tv.get_children(), (item_id, ))
+        self.failUnlessEqual(self.tv.get_children(item_id), (item2, ))
+
+        self.tv.delete(item_id)
+        self.failIf(self.tv.get_children())
+
+        # reattach should fail
+        self.failUnlessRaises(tkinter.TclError,
+            self.tv.reattach, item_id, '', 'end')
+
+
+    def test_detach_reattach(self):
+        item_id = self.tv.insert('', 'end')
+        item2 = self.tv.insert(item_id, 'end')
+
+        self.failUnlessEqual(self.tv.get_children(), (item_id, ))
+        self.failUnlessEqual(self.tv.get_children(item_id), (item2, ))
+
+        # detach item with children
+        self.tv.detach(item_id)
+        self.failIf(self.tv.get_children())
+
+        # reattach item with children
+        self.tv.reattach(item_id, '', 'end')
+        self.failUnlessEqual(self.tv.get_children(), (item_id, ))
+        self.failUnlessEqual(self.tv.get_children(item_id), (item2, ))
+
+        # move a children to the root
+        self.tv.move(item2, '', 'end')
+        self.failUnlessEqual(self.tv.get_children(), (item_id, item2))
+        self.failUnlessEqual(self.tv.get_children(item_id), ())
+
+        # bad values
+        self.failUnlessRaises(tkinter.TclError,
+            self.tv.reattach, 'nonexistant', '', 'end')
+        self.failUnlessRaises(tkinter.TclError,
+            self.tv.detach, 'nonexistant')
+        self.failUnlessRaises(tkinter.TclError,
+            self.tv.reattach, item2, 'otherparent', 'end')
+        self.failUnlessRaises(tkinter.TclError,
+            self.tv.reattach, item2, '', 'invalid')
+
+        # multiple detach
+        self.tv.detach(item_id, item2)
+        self.failUnlessEqual(self.tv.get_children(), ())
+        self.failUnlessEqual(self.tv.get_children(item_id), ())
+
+
+def test_main():
+    support.run(TreeviewTest)
+
+if __name__ == "__main__":
+    test_main()

Modified: sandbox/trunk/ttk-gsoc/src/3.x/ttk.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/3.x/ttk.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/3.x/ttk.py	Fri Aug 22 18:18:01 2008
@@ -1154,11 +1154,11 @@
     def get_children(self, item=None):
         """Returns a tuple of children belonging to item.
         
-        If item is not specified, returns all root children."""
-        return self.tk.call(self._w, "children", item or '')
+        If item is not specified, returns root children."""
+        return self.tk.call(self._w, "children", item or '') or ()
 
 
-    def set_children(self, item, newchildren):
+    def set_children(self, item, *newchildren):
         """Replaces item's child with newchildren.
 
         Children present in item that are not present in newchildren
@@ -1183,7 +1183,7 @@
         self.tk.call(self._w, "delete", items)
 
 
-    def detach(self, items):
+    def detach(self, *items):
         """Unlinks all of the specified items from the tree.
 
         The items and all of their descendants are still present, and may


More information about the Python-checkins mailing list