Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 07bcf14

Browse files
Issue #6181: Fixed minor bugs in tkinter.Listbox methods:
bbox(), curselection() and get().
2 parents 5631e1f + fc14ad9 commit 07bcf14

4 files changed

Lines changed: 60 additions & 29 deletions

File tree

Lib/tkinter/__init__.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2591,22 +2591,19 @@ def __init__(self, master=None, cnf={}, **kw):
25912591
def activate(self, index):
25922592
"""Activate item identified by INDEX."""
25932593
self.tk.call(self._w, 'activate', index)
2594-
def bbox(self, *args):
2594+
def bbox(self, index):
25952595
"""Return a tuple of X1,Y1,X2,Y2 coordinates for a rectangle
2596-
which encloses the item identified by index in ARGS."""
2597-
return self._getints(
2598-
self.tk.call((self._w, 'bbox') + args)) or None
2596+
which encloses the item identified by the given index."""
2597+
return self._getints(self.tk.call(self._w, 'bbox', index)) or None
25992598
def curselection(self):
2600-
"""Return list of indices of currently selected item."""
2601-
# XXX Ought to apply self._getints()...
2602-
return self.tk.splitlist(self.tk.call(
2603-
self._w, 'curselection'))
2599+
"""Return the indices of currently selected item."""
2600+
return self._getints(self.tk.call(self._w, 'curselection')) or ()
26042601
def delete(self, first, last=None):
26052602
"""Delete items from FIRST to LAST (included)."""
26062603
self.tk.call(self._w, 'delete', first, last)
26072604
def get(self, first, last=None):
26082605
"""Get list of items from FIRST to LAST (included)."""
2609-
if last:
2606+
if last is not None:
26102607
return self.tk.splitlist(self.tk.call(
26112608
self._w, 'get', first, last))
26122609
else:

Lib/tkinter/test/test_tkinter/test_widgets.py

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,7 @@ def test_wrap(self):
467467

468468
def test_bbox(self):
469469
widget = self.create()
470-
bbox = widget.bbox(0)
471-
self.assertEqual(len(bbox), 4)
472-
for item in bbox:
473-
self.assertIsInstance(item, int)
474-
470+
self.assertIsBoundingBox(widget.bbox(0))
475471
self.assertRaises(tkinter.TclError, widget.bbox, 'noindex')
476472
self.assertRaises(tkinter.TclError, widget.bbox, None)
477473
self.assertRaises(TypeError, widget.bbox)
@@ -624,11 +620,7 @@ def test_wrap(self):
624620

625621
def test_bbox(self):
626622
widget = self.create()
627-
bbox = widget.bbox('1.1')
628-
self.assertEqual(len(bbox), 4)
629-
for item in bbox:
630-
self.assertIsInstance(item, int)
631-
623+
self.assertIsBoundingBox(widget.bbox('1.1'))
632624
self.assertIsNone(widget.bbox('end'))
633625
self.assertRaises(tkinter.TclError, widget.bbox, 'noindex')
634626
self.assertRaises(tkinter.TclError, widget.bbox, None)
@@ -785,6 +777,46 @@ def test_itemconfigure_selectbackground(self):
785777
def test_itemconfigure_selectforeground(self):
786778
self.check_itemconfigure('selectforeground', '#654321')
787779

780+
def test_box(self):
781+
lb = self.create()
782+
lb.insert(0, *('el%d' % i for i in range(8)))
783+
lb.pack()
784+
self.assertIsBoundingBox(lb.bbox(0))
785+
self.assertIsNone(lb.bbox(-1))
786+
self.assertIsNone(lb.bbox(10))
787+
self.assertRaises(TclError, lb.bbox, 'noindex')
788+
self.assertRaises(TclError, lb.bbox, None)
789+
self.assertRaises(TypeError, lb.bbox)
790+
self.assertRaises(TypeError, lb.bbox, 0, 1)
791+
792+
def test_curselection(self):
793+
lb = self.create()
794+
lb.insert(0, *('el%d' % i for i in range(8)))
795+
lb.selection_clear(0, tkinter.END)
796+
lb.selection_set(2, 4)
797+
lb.selection_set(6)
798+
self.assertEqual(lb.curselection(), (2, 3, 4, 6))
799+
self.assertRaises(TypeError, lb.curselection, 0)
800+
801+
def test_get(self):
802+
lb = self.create()
803+
lb.insert(0, *('el%d' % i for i in range(8)))
804+
self.assertEqual(lb.get(0), 'el0')
805+
self.assertEqual(lb.get(3), 'el3')
806+
self.assertEqual(lb.get('end'), 'el7')
807+
self.assertEqual(lb.get(8), '')
808+
self.assertEqual(lb.get(-1), '')
809+
self.assertEqual(lb.get(3, 5), ('el3', 'el4', 'el5'))
810+
self.assertEqual(lb.get(5, 'end'), ('el5', 'el6', 'el7'))
811+
self.assertEqual(lb.get(5, 0), ())
812+
self.assertEqual(lb.get(0, 0), ('el0',))
813+
self.assertRaises(TclError, lb.get, 'noindex')
814+
self.assertRaises(TclError, lb.get, None)
815+
self.assertRaises(TypeError, lb.get)
816+
self.assertRaises(TclError, lb.get, 'end', 'noindex')
817+
self.assertRaises(TypeError, lb.get, 1, 2, 3)
818+
self.assertRaises(TclError, lb.get, 2.4)
819+
788820

789821
@add_standard_options(PixelSizeTests, StandardOptionsTests)
790822
class ScaleTest(AbstractWidgetTest, unittest.TestCase):

Lib/tkinter/test/test_ttk/test_widgets.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,7 @@ def test_validatecommand(self):
460460

461461

462462
def test_bbox(self):
463-
self.assertEqual(len(self.entry.bbox(0)), 4)
464-
for item in self.entry.bbox(0):
465-
self.assertIsInstance(item, int)
466-
463+
self.assertIsBoundingBox(self.entry.bbox(0))
467464
self.assertRaises(tkinter.TclError, self.entry.bbox, 'noindex')
468465
self.assertRaises(tkinter.TclError, self.entry.bbox, None)
469466

@@ -1216,12 +1213,7 @@ def test_bbox(self):
12161213
self.assertTrue(children)
12171214

12181215
bbox = self.tv.bbox(children[0])
1219-
self.assertEqual(len(bbox), 4)
1220-
self.assertIsInstance(bbox, tuple)
1221-
for item in bbox:
1222-
if not isinstance(item, int):
1223-
self.fail("Invalid bounding box: %s" % bbox)
1224-
break
1216+
self.assertIsBoundingBox(bbox)
12251217

12261218
# compare width in bboxes
12271219
self.tv['columns'] = ['test']

Lib/tkinter/test/widget_tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,16 @@ def checkImageParam(self, widget, name):
202202
def checkVariableParam(self, widget, name, var):
203203
self.checkParam(widget, name, var, conv=str)
204204

205+
def assertIsBoundingBox(self, bbox):
206+
self.assertIsNotNone(bbox)
207+
self.assertIsInstance(bbox, tuple)
208+
if len(bbox) != 4:
209+
self.fail('Invalid bounding box: %r' % (bbox,))
210+
for item in bbox:
211+
if not isinstance(item, int):
212+
self.fail('Invalid bounding box: %r' % (bbox,))
213+
break
214+
205215

206216
class StandardOptionsTests:
207217
STANDARD_OPTIONS = (

0 commit comments

Comments
 (0)