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.
This commit is contained in:
Juliette Monsel 2018-10-08 18:29:24 +02:00 committed by Serhiy Storchaka
parent a8d5e2f255
commit af5658ae93
5 changed files with 82 additions and 0 deletions

View File

@ -167,6 +167,16 @@ Added :attr:`SSLContext.post_handshake_auth` to enable and
post-handshake authentication. post-handshake authentication.
(Contributed by Christian Heimes in :issue:`34670`.) (Contributed by Christian Heimes in :issue:`34670`.)
tkinter
-------
Added methods :meth:`~tkinter.Spinbox.selection_from`,
:meth:`~tkinter.Spinbox.selection_present`,
:meth:`~tkinter.Spinbox.selection_range` and
:meth:`~tkinter.Spinbox.selection_to`
in the :class:`tkinter.Spinbox` class.
(Contributed by Juliette Monsel in :issue:`34829`.)
venv venv
---- ----

View File

@ -3770,6 +3770,24 @@ class Spinbox(Widget, XView):
""" """
return self.selection("element", element) return self.selection("element", element)
def selection_from(self, index):
"""Set the fixed end of a selection to INDEX."""
self.selection('from', index)
def selection_present(self):
"""Return True if there are characters selected in the spinbox, False
otherwise."""
return self.tk.getboolean(
self.tk.call(self._w, 'selection', 'present'))
def selection_range(self, start, end):
"""Set the selection from START to END (not included)."""
self.selection('range', start, end)
def selection_to(self, index):
"""Set the variable end of a selection to INDEX."""
self.selection('to', index)
########################################################################### ###########################################################################
class LabelFrame(Widget): class LabelFrame(Widget):

View File

@ -377,6 +377,31 @@ class EntryTest(AbstractWidgetTest, unittest.TestCase):
self.checkCommandParam(widget, 'validatecommand') self.checkCommandParam(widget, 'validatecommand')
self.checkCommandParam(widget, 'vcmd') self.checkCommandParam(widget, 'vcmd')
def test_selection_methods(self):
widget = self.create()
widget.insert(0, '12345')
self.assertFalse(widget.selection_present())
widget.selection_range(0, 'end')
self.assertEqual(widget.selection_get(), '12345')
self.assertTrue(widget.selection_present())
widget.selection_from(1)
widget.selection_to(2)
self.assertEqual(widget.selection_get(), '2')
widget.selection_range(3, 4)
self.assertEqual(widget.selection_get(), '4')
widget.selection_clear()
self.assertFalse(widget.selection_present())
widget.selection_range(0, 'end')
widget.selection_adjust(4)
self.assertEqual(widget.selection_get(), '1234')
widget.selection_adjust(1)
self.assertEqual(widget.selection_get(), '234')
widget.selection_adjust(5)
self.assertEqual(widget.selection_get(), '2345')
widget.selection_adjust(0)
self.assertEqual(widget.selection_get(), '12345')
widget.selection_adjust(0)
@add_standard_options(StandardOptionsTests) @add_standard_options(StandardOptionsTests)
class SpinboxTest(EntryTest, unittest.TestCase): class SpinboxTest(EntryTest, unittest.TestCase):
@ -474,6 +499,31 @@ class SpinboxTest(EntryTest, unittest.TestCase):
self.assertRaises(TypeError, widget.bbox) self.assertRaises(TypeError, widget.bbox)
self.assertRaises(TypeError, widget.bbox, 0, 1) self.assertRaises(TypeError, widget.bbox, 0, 1)
def test_selection_methods(self):
widget = self.create()
widget.insert(0, '12345')
self.assertFalse(widget.selection_present())
widget.selection_range(0, 'end')
self.assertEqual(widget.selection_get(), '12345')
self.assertTrue(widget.selection_present())
widget.selection_from(1)
widget.selection_to(2)
self.assertEqual(widget.selection_get(), '2')
widget.selection_range(3, 4)
self.assertEqual(widget.selection_get(), '4')
widget.selection_clear()
self.assertFalse(widget.selection_present())
widget.selection_range(0, 'end')
widget.selection_adjust(4)
self.assertEqual(widget.selection_get(), '1234')
widget.selection_adjust(1)
self.assertEqual(widget.selection_get(), '234')
widget.selection_adjust(5)
self.assertEqual(widget.selection_get(), '2345')
widget.selection_adjust(0)
self.assertEqual(widget.selection_get(), '12345')
widget.selection_adjust(0)
@add_standard_options(StandardOptionsTests) @add_standard_options(StandardOptionsTests)
class TextTest(AbstractWidgetTest, unittest.TestCase): class TextTest(AbstractWidgetTest, unittest.TestCase):

View File

@ -1094,6 +1094,7 @@ Tim Mitchell
Zubin Mithra Zubin Mithra
Florian Mladitsch Florian Mladitsch
Doug Moen Doug Moen
Juliette Monsel
The Dragon De Monsyne The Dragon De Monsyne
Bastien Montagne Bastien Montagne
Skip Montanaro Skip Montanaro

View File

@ -0,0 +1,3 @@
Add methods ``selection_from``, ``selection_range``, ``selection_present``
and ``selection_to`` to the ``tkinter.Spinbox`` for consistency with the
``tkinter.Entry`` widget. Patch by Juliette Monsel.