Skip to content

Commit af5658a

Browse files
j4321serhiy-storchaka
authored andcommitted
bpo-34829: Add missing selection_ methods to the Tkinter Spinbox. (GH-9617)
Implement the methods selection_from(), selection_range(), selection_present() and selection_to() for Tkinter Spinbox.
1 parent a8d5e2f commit af5658a

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

Doc/whatsnew/3.8.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ Added :attr:`SSLContext.post_handshake_auth` to enable and
167167
post-handshake authentication.
168168
(Contributed by Christian Heimes in :issue:`34670`.)
169169

170+
tkinter
171+
-------
172+
173+
Added methods :meth:`~tkinter.Spinbox.selection_from`,
174+
:meth:`~tkinter.Spinbox.selection_present`,
175+
:meth:`~tkinter.Spinbox.selection_range` and
176+
:meth:`~tkinter.Spinbox.selection_to`
177+
in the :class:`tkinter.Spinbox` class.
178+
(Contributed by Juliette Monsel in :issue:`34829`.)
179+
170180
venv
171181
----
172182

Lib/tkinter/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3770,6 +3770,24 @@ def selection_element(self, element=None):
37703770
"""
37713771
return self.selection("element", element)
37723772

3773+
def selection_from(self, index):
3774+
"""Set the fixed end of a selection to INDEX."""
3775+
self.selection('from', index)
3776+
3777+
def selection_present(self):
3778+
"""Return True if there are characters selected in the spinbox, False
3779+
otherwise."""
3780+
return self.tk.getboolean(
3781+
self.tk.call(self._w, 'selection', 'present'))
3782+
3783+
def selection_range(self, start, end):
3784+
"""Set the selection from START to END (not included)."""
3785+
self.selection('range', start, end)
3786+
3787+
def selection_to(self, index):
3788+
"""Set the variable end of a selection to INDEX."""
3789+
self.selection('to', index)
3790+
37733791
###########################################################################
37743792

37753793
class LabelFrame(Widget):

Lib/tkinter/test/test_tkinter/test_widgets.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,31 @@ def test_validatecommand(self):
377377
self.checkCommandParam(widget, 'validatecommand')
378378
self.checkCommandParam(widget, 'vcmd')
379379

380+
def test_selection_methods(self):
381+
widget = self.create()
382+
widget.insert(0, '12345')
383+
self.assertFalse(widget.selection_present())
384+
widget.selection_range(0, 'end')
385+
self.assertEqual(widget.selection_get(), '12345')
386+
self.assertTrue(widget.selection_present())
387+
widget.selection_from(1)
388+
widget.selection_to(2)
389+
self.assertEqual(widget.selection_get(), '2')
390+
widget.selection_range(3, 4)
391+
self.assertEqual(widget.selection_get(), '4')
392+
widget.selection_clear()
393+
self.assertFalse(widget.selection_present())
394+
widget.selection_range(0, 'end')
395+
widget.selection_adjust(4)
396+
self.assertEqual(widget.selection_get(), '1234')
397+
widget.selection_adjust(1)
398+
self.assertEqual(widget.selection_get(), '234')
399+
widget.selection_adjust(5)
400+
self.assertEqual(widget.selection_get(), '2345')
401+
widget.selection_adjust(0)
402+
self.assertEqual(widget.selection_get(), '12345')
403+
widget.selection_adjust(0)
404+
380405

381406
@add_standard_options(StandardOptionsTests)
382407
class SpinboxTest(EntryTest, unittest.TestCase):
@@ -474,6 +499,31 @@ def test_bbox(self):
474499
self.assertRaises(TypeError, widget.bbox)
475500
self.assertRaises(TypeError, widget.bbox, 0, 1)
476501

502+
def test_selection_methods(self):
503+
widget = self.create()
504+
widget.insert(0, '12345')
505+
self.assertFalse(widget.selection_present())
506+
widget.selection_range(0, 'end')
507+
self.assertEqual(widget.selection_get(), '12345')
508+
self.assertTrue(widget.selection_present())
509+
widget.selection_from(1)
510+
widget.selection_to(2)
511+
self.assertEqual(widget.selection_get(), '2')
512+
widget.selection_range(3, 4)
513+
self.assertEqual(widget.selection_get(), '4')
514+
widget.selection_clear()
515+
self.assertFalse(widget.selection_present())
516+
widget.selection_range(0, 'end')
517+
widget.selection_adjust(4)
518+
self.assertEqual(widget.selection_get(), '1234')
519+
widget.selection_adjust(1)
520+
self.assertEqual(widget.selection_get(), '234')
521+
widget.selection_adjust(5)
522+
self.assertEqual(widget.selection_get(), '2345')
523+
widget.selection_adjust(0)
524+
self.assertEqual(widget.selection_get(), '12345')
525+
widget.selection_adjust(0)
526+
477527

478528
@add_standard_options(StandardOptionsTests)
479529
class TextTest(AbstractWidgetTest, unittest.TestCase):

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,7 @@ Tim Mitchell
10941094
Zubin Mithra
10951095
Florian Mladitsch
10961096
Doug Moen
1097+
Juliette Monsel
10971098
The Dragon De Monsyne
10981099
Bastien Montagne
10991100
Skip Montanaro
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add methods ``selection_from``, ``selection_range``, ``selection_present``
2+
and ``selection_to`` to the ``tkinter.Spinbox`` for consistency with the
3+
``tkinter.Entry`` widget. Patch by Juliette Monsel.

0 commit comments

Comments
 (0)