Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
tkinter.test.test_ttk.test_widgets.TreeviewTest Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def test_bbox
def test_children
def test_column
def test_delete
def test_detach_reattach
def test_exists
def test_focus
def test_heading
def test_heading_callback
def test_index
def test_insert_item
def test_set
def test_tag_bind
def test_tag_configure

Public Attributes

 tv

Detailed Description

Definition at line 741 of file test_widgets.py.


Member Function Documentation

Definition at line 743 of file test_widgets.py.

00743 
00744     def setUp(self):
00745         support.root_deiconify()
00746         self.tv = ttk.Treeview(padding=0)

Here is the caller graph for this function:

Definition at line 747 of file test_widgets.py.

00747 
00748     def tearDown(self):
00749         self.tv.destroy()
00750         support.root_withdraw()
00751 

Here is the caller graph for this function:

Definition at line 752 of file test_widgets.py.

00752 
00753     def test_bbox(self):
00754         self.tv.pack()
00755         self.assertEqual(self.tv.bbox(''), '')
00756         self.tv.wait_visibility()
00757         self.tv.update()
00758 
00759         item_id = self.tv.insert('', 'end')
00760         children = self.tv.get_children()
00761         self.assertTrue(children)
00762 
00763         bbox = self.tv.bbox(children[0])
00764         self.assertEqual(len(bbox), 4)
00765         self.assertTrue(isinstance(bbox, tuple))
00766         for item in bbox:
00767             if not isinstance(item, int):
00768                 self.fail("Invalid bounding box: %s" % bbox)
00769                 break
00770 
00771         # compare width in bboxes
00772         self.tv['columns'] = ['test']
00773         self.tv.column('test', width=50)
00774         bbox_column0 = self.tv.bbox(children[0], 0)
00775         root_width = self.tv.column('#0', width=None)
00776         self.assertEqual(bbox_column0[0], bbox[0] + root_width)
00777 
00778         # verify that bbox of a closed item is the empty string
00779         child1 = self.tv.insert(item_id, 'end')
00780         self.assertEqual(self.tv.bbox(child1), '')
00781 

Here is the call graph for this function:

Definition at line 782 of file test_widgets.py.

00782 
00783     def test_children(self):
00784         # no children yet, should get an empty tuple
00785         self.assertEqual(self.tv.get_children(), ())
00786 
00787         item_id = self.tv.insert('', 'end')
00788         self.assertTrue(isinstance(self.tv.get_children(), tuple))
00789         self.assertEqual(self.tv.get_children()[0], item_id)
00790 
00791         # add item_id and child3 as children of child2
00792         child2 = self.tv.insert('', 'end')
00793         child3 = self.tv.insert('', 'end')
00794         self.tv.set_children(child2, item_id, child3)
00795         self.assertEqual(self.tv.get_children(child2), (item_id, child3))
00796 
00797         # child3 has child2 as parent, thus trying to set child2 as a children
00798         # of child3 should result in an error
00799         self.assertRaises(tkinter.TclError,
00800             self.tv.set_children, child3, child2)
00801 
00802         # remove child2 children
00803         self.tv.set_children(child2)
00804         self.assertEqual(self.tv.get_children(child2), ())
00805 
00806         # remove root's children
00807         self.tv.set_children('')
00808         self.assertEqual(self.tv.get_children(), ())
00809 

Here is the call graph for this function:

Definition at line 810 of file test_widgets.py.

00810 
00811     def test_column(self):
00812         # return a dict with all options/values
00813         self.assertTrue(isinstance(self.tv.column('#0'), dict))
00814         # return a single value of the given option
00815         self.assertTrue(isinstance(self.tv.column('#0', width=None), int))
00816         # set a new value for an option
00817         self.tv.column('#0', width=10)
00818         # testing new way to get option value
00819         self.assertEqual(self.tv.column('#0', 'width'), 10)
00820         self.assertEqual(self.tv.column('#0', width=None), 10)
00821         # check read-only option
00822         self.assertRaises(tkinter.TclError, self.tv.column, '#0', id='X')
00823 
00824         self.assertRaises(tkinter.TclError, self.tv.column, 'invalid')
00825         invalid_kws = [
00826             {'unknown_option': 'some value'},  {'stretch': 'wrong'},
00827             {'anchor': 'wrong'}, {'width': 'wrong'}, {'minwidth': 'wrong'}
00828         ]
00829         for kw in invalid_kws:
00830             self.assertRaises(tkinter.TclError, self.tv.column, '#0',
00831                 **kw)
00832 

Here is the call graph for this function:

Definition at line 833 of file test_widgets.py.

00833 
00834     def test_delete(self):
00835         self.assertRaises(tkinter.TclError, self.tv.delete, '#0')
00836 
00837         item_id = self.tv.insert('', 'end')
00838         item2 = self.tv.insert(item_id, 'end')
00839         self.assertEqual(self.tv.get_children(), (item_id, ))
00840         self.assertEqual(self.tv.get_children(item_id), (item2, ))
00841 
00842         self.tv.delete(item_id)
00843         self.assertFalse(self.tv.get_children())
00844 
00845         # reattach should fail
00846         self.assertRaises(tkinter.TclError,
00847             self.tv.reattach, item_id, '', 'end')
00848 
00849         # test multiple item delete
00850         item1 = self.tv.insert('', 'end')
00851         item2 = self.tv.insert('', 'end')
00852         self.assertEqual(self.tv.get_children(), (item1, item2))
00853 
00854         self.tv.delete(item1, item2)
00855         self.assertFalse(self.tv.get_children())
00856 

Here is the call graph for this function:

Definition at line 857 of file test_widgets.py.

00857 
00858     def test_detach_reattach(self):
00859         item_id = self.tv.insert('', 'end')
00860         item2 = self.tv.insert(item_id, 'end')
00861 
00862         # calling detach without items is valid, although it does nothing
00863         prev = self.tv.get_children()
00864         self.tv.detach() # this should do nothing
00865         self.assertEqual(prev, self.tv.get_children())
00866 
00867         self.assertEqual(self.tv.get_children(), (item_id, ))
00868         self.assertEqual(self.tv.get_children(item_id), (item2, ))
00869 
00870         # detach item with children
00871         self.tv.detach(item_id)
00872         self.assertFalse(self.tv.get_children())
00873 
00874         # reattach item with children
00875         self.tv.reattach(item_id, '', 'end')
00876         self.assertEqual(self.tv.get_children(), (item_id, ))
00877         self.assertEqual(self.tv.get_children(item_id), (item2, ))
00878 
00879         # move a children to the root
00880         self.tv.move(item2, '', 'end')
00881         self.assertEqual(self.tv.get_children(), (item_id, item2))
00882         self.assertEqual(self.tv.get_children(item_id), ())
00883 
00884         # bad values
00885         self.assertRaises(tkinter.TclError,
00886             self.tv.reattach, 'nonexistent', '', 'end')
00887         self.assertRaises(tkinter.TclError,
00888             self.tv.detach, 'nonexistent')
00889         self.assertRaises(tkinter.TclError,
00890             self.tv.reattach, item2, 'otherparent', 'end')
00891         self.assertRaises(tkinter.TclError,
00892             self.tv.reattach, item2, '', 'invalid')
00893 
00894         # multiple detach
00895         self.tv.detach(item_id, item2)
00896         self.assertEqual(self.tv.get_children(), ())
00897         self.assertEqual(self.tv.get_children(item_id), ())
00898 

Here is the call graph for this function:

Definition at line 899 of file test_widgets.py.

00899 
00900     def test_exists(self):
00901         self.assertEqual(self.tv.exists('something'), False)
00902         self.assertEqual(self.tv.exists(''), True)
00903         self.assertEqual(self.tv.exists({}), False)
00904 
00905         # the following will make a tk.call equivalent to
00906         # tk.call(treeview, "exists") which should result in an error
00907         # in the tcl interpreter since tk requires an item.
00908         self.assertRaises(tkinter.TclError, self.tv.exists, None)
00909 

Here is the call graph for this function:

Definition at line 910 of file test_widgets.py.

00910 
00911     def test_focus(self):
00912         # nothing is focused right now
00913         self.assertEqual(self.tv.focus(), '')
00914 
00915         item1 = self.tv.insert('', 'end')
00916         self.tv.focus(item1)
00917         self.assertEqual(self.tv.focus(), item1)
00918 
00919         self.tv.delete(item1)
00920         self.assertEqual(self.tv.focus(), '')
00921 
00922         # try focusing inexistent item
00923         self.assertRaises(tkinter.TclError, self.tv.focus, 'hi')
00924 

Here is the call graph for this function:

Definition at line 925 of file test_widgets.py.

00925 
00926     def test_heading(self):
00927         # check a dict is returned
00928         self.assertTrue(isinstance(self.tv.heading('#0'), dict))
00929 
00930         # check a value is returned
00931         self.tv.heading('#0', text='hi')
00932         self.assertEqual(self.tv.heading('#0', 'text'), 'hi')
00933         self.assertEqual(self.tv.heading('#0', text=None), 'hi')
00934 
00935         # invalid option
00936         self.assertRaises(tkinter.TclError, self.tv.heading, '#0',
00937             background=None)
00938         # invalid value
00939         self.assertRaises(tkinter.TclError, self.tv.heading, '#0',
00940             anchor=1)

Here is the call graph for this function:

Definition at line 943 of file test_widgets.py.

00943 
00944     def test_heading_callback(self):
00945         def simulate_heading_click(x, y):
00946             support.simulate_mouse_click(self.tv, x, y)
00947             self.tv.update_idletasks()
00948 
00949         success = [] # no success for now
00950 
00951         self.tv.pack()
00952         self.tv.wait_visibility()
00953         self.tv.heading('#0', command=lambda: success.append(True))
00954         self.tv.column('#0', width=100)
00955         self.tv.update()
00956 
00957         # assuming that the coords (5, 5) fall into heading #0
00958         simulate_heading_click(5, 5)
00959         if not success:
00960             self.fail("The command associated to the treeview heading wasn't "
00961                 "invoked.")
00962 
00963         success = []
00964         commands = self.tv.master._tclCommands
00965         self.tv.heading('#0', command=str(self.tv.heading('#0', command=None)))
00966         self.assertEqual(commands, self.tv.master._tclCommands)
00967         simulate_heading_click(5, 5)
00968         if not success:
00969             self.fail("The command associated to the treeview heading wasn't "
00970                 "invoked.")
00971 
00972         # XXX The following raises an error in a tcl interpreter, but not in
00973         # Python
00974         #self.tv.heading('#0', command='I dont exist')
00975         #simulate_heading_click(5, 5)
00976 

Here is the call graph for this function:

Definition at line 977 of file test_widgets.py.

00977 
00978     def test_index(self):
00979         # item 'what' doesn't exist
00980         self.assertRaises(tkinter.TclError, self.tv.index, 'what')
00981 
00982         self.assertEqual(self.tv.index(''), 0)
00983 
00984         item1 = self.tv.insert('', 'end')
00985         item2 = self.tv.insert('', 'end')
00986         c1 = self.tv.insert(item1, 'end')
00987         c2 = self.tv.insert(item1, 'end')
00988         self.assertEqual(self.tv.index(item1), 0)
00989         self.assertEqual(self.tv.index(c1), 0)
00990         self.assertEqual(self.tv.index(c2), 1)
00991         self.assertEqual(self.tv.index(item2), 1)
00992 
00993         self.tv.move(item2, '', 0)
00994         self.assertEqual(self.tv.index(item2), 0)
00995         self.assertEqual(self.tv.index(item1), 1)
00996 
00997         # check that index still works even after its parent and siblings
00998         # have been detached
00999         self.tv.detach(item1)
01000         self.assertEqual(self.tv.index(c2), 1)
01001         self.tv.detach(c1)
01002         self.assertEqual(self.tv.index(c2), 0)
01003 
01004         # but it fails after item has been deleted
01005         self.tv.delete(item1)
01006         self.assertRaises(tkinter.TclError, self.tv.index, c2)
01007 

Here is the call graph for this function:

Definition at line 1008 of file test_widgets.py.

01008 
01009     def test_insert_item(self):
01010         # parent 'none' doesn't exist
01011         self.assertRaises(tkinter.TclError, self.tv.insert, 'none', 'end')
01012 
01013         # open values
01014         self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end',
01015             open='')
01016         self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end',
01017             open='please')
01018         self.assertFalse(self.tv.delete(self.tv.insert('', 'end', open=True)))
01019         self.assertFalse(self.tv.delete(self.tv.insert('', 'end', open=False)))
01020 
01021         # invalid index
01022         self.assertRaises(tkinter.TclError, self.tv.insert, '', 'middle')
01023 
01024         # trying to duplicate item id is invalid
01025         itemid = self.tv.insert('', 'end', 'first-item')
01026         self.assertEqual(itemid, 'first-item')
01027         self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end',
01028             'first-item')
01029         self.assertRaises(tkinter.TclError, self.tv.insert, '', 'end',
01030             MockTclObj('first-item'))
01031 
01032         # unicode values
01033         value = '\xe1ba'
01034         item = self.tv.insert('', 'end', values=(value, ))
01035         self.assertEqual(self.tv.item(item, 'values'), (value, ))
01036         self.assertEqual(self.tv.item(item, values=None), (value, ))
01037 
01038         self.tv.item(item, values=list(self.tv.item(item, values=None)))
01039         self.assertEqual(self.tv.item(item, values=None), (value, ))
01040 
01041         self.assertTrue(isinstance(self.tv.item(item), dict))
01042 
01043         # erase item values
01044         self.tv.item(item, values='')
01045         self.assertFalse(self.tv.item(item, values=None))
01046 
01047         # item tags
01048         item = self.tv.insert('', 'end', tags=[1, 2, value])
01049         self.assertEqual(self.tv.item(item, tags=None), ('1', '2', value))
01050         self.tv.item(item, tags=[])
01051         self.assertFalse(self.tv.item(item, tags=None))
01052         self.tv.item(item, tags=(1, 2))
01053         self.assertEqual(self.tv.item(item, tags=None), ('1', '2'))
01054 
01055         # values with spaces
01056         item = self.tv.insert('', 'end', values=('a b c',
01057             '%s %s' % (value, value)))
01058         self.assertEqual(self.tv.item(item, values=None),
01059             ('a b c', '%s %s' % (value, value)))
01060 
01061         # text
01062         self.assertEqual(self.tv.item(
01063             self.tv.insert('', 'end', text="Label here"), text=None),
01064             "Label here")
01065         self.assertEqual(self.tv.item(
01066             self.tv.insert('', 'end', text=value), text=None),
01067             value)
01068 

Here is the call graph for this function:

Definition at line 1069 of file test_widgets.py.

01069 
01070     def test_set(self):
01071         self.tv['columns'] = ['A', 'B']
01072         item = self.tv.insert('', 'end', values=['a', 'b'])
01073         self.assertEqual(self.tv.set(item), {'A': 'a', 'B': 'b'})
01074 
01075         self.tv.set(item, 'B', 'a')
01076         self.assertEqual(self.tv.item(item, values=None), ('a', 'a'))
01077 
01078         self.tv['columns'] = ['B']
01079         self.assertEqual(self.tv.set(item), {'B': 'a'})
01080 
01081         self.tv.set(item, 'B', 'b')
01082         self.assertEqual(self.tv.set(item, column='B'), 'b')
01083         self.assertEqual(self.tv.item(item, values=None), ('b', 'a'))
01084 
01085         self.tv.set(item, 'B', 123)
01086         self.assertEqual(self.tv.set(item, 'B'), 123)
01087         self.assertEqual(self.tv.item(item, values=None), (123, 'a'))
01088         self.assertEqual(self.tv.set(item), {'B': 123})
01089 
01090         # inexistent column
01091         self.assertRaises(tkinter.TclError, self.tv.set, item, 'A')
01092         self.assertRaises(tkinter.TclError, self.tv.set, item, 'A', 'b')
01093 
01094         # inexistent item
01095         self.assertRaises(tkinter.TclError, self.tv.set, 'notme')
01096 

Here is the call graph for this function:

Definition at line 1097 of file test_widgets.py.

01097 
01098     def test_tag_bind(self):
01099         events = []
01100         item1 = self.tv.insert('', 'end', tags=['call'])
01101         item2 = self.tv.insert('', 'end', tags=['call'])
01102         self.tv.tag_bind('call', '<ButtonPress-1>',
01103             lambda evt: events.append(1))
01104         self.tv.tag_bind('call', '<ButtonRelease-1>',
01105             lambda evt: events.append(2))
01106 
01107         self.tv.pack()
01108         self.tv.wait_visibility()
01109         self.tv.update()
01110 
01111         pos_y = set()
01112         found = set()
01113         for i in range(0, 100, 10):
01114             if len(found) == 2: # item1 and item2 already found
01115                 break
01116             item_id = self.tv.identify_row(i)
01117             if item_id and item_id not in found:
01118                 pos_y.add(i)
01119                 found.add(item_id)
01120 
01121         self.assertEqual(len(pos_y), 2) # item1 and item2 y pos
01122         for y in pos_y:
01123             support.simulate_mouse_click(self.tv, 0, y)
01124 
01125         # by now there should be 4 things in the events list, since each
01126         # item had a bind for two events that were simulated above
01127         self.assertEqual(len(events), 4)
01128         for evt in zip(events[::2], events[1::2]):
01129             self.assertEqual(evt, (1, 2))
01130 

Here is the call graph for this function:

Definition at line 1131 of file test_widgets.py.

01131 
01132     def test_tag_configure(self):
01133         # Just testing parameter passing for now
01134         self.assertRaises(TypeError, self.tv.tag_configure)
01135         self.assertRaises(tkinter.TclError, self.tv.tag_configure,
01136             'test', sky='blue')
01137         self.tv.tag_configure('test', foreground='blue')
01138         self.assertEqual(str(self.tv.tag_configure('test', 'foreground')),
01139             'blue')
01140         self.assertEqual(str(self.tv.tag_configure('test', foreground=None)),
01141             'blue')
01142         self.assertTrue(isinstance(self.tv.tag_configure('test'), dict))
01143 

Here is the call graph for this function:


Member Data Documentation

Definition at line 745 of file test_widgets.py.


The documentation for this class was generated from the following file: