[Python-checkins] bpo-34829: Add missing selection_ methods to the Tkinter Spinbox. (GH-9617)

Serhiy Storchaka webhook-mailer at python.org
Mon Oct 8 12:29:34 EDT 2018


https://github.com/python/cpython/commit/af5658ae93b0a87ab4420a7dc30a07fa5a83e252
commit: af5658ae93b0a87ab4420a7dc30a07fa5a83e252
branch: master
author: Juliette Monsel <j4321 at users.noreply.github.com>
committer: Serhiy Storchaka <storchaka at gmail.com>
date: 2018-10-08T19:29:24+03:00
summary:

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.

files:
A Misc/NEWS.d/next/Library/2018-10-08-16-04-36.bpo-34829.B7v7D0.rst
M Doc/whatsnew/3.8.rst
M Lib/tkinter/__init__.py
M Lib/tkinter/test/test_tkinter/test_widgets.py
M Misc/ACKS

diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst
index a146249178f4..d9c3f1bd7dd6 100644
--- a/Doc/whatsnew/3.8.rst
+++ b/Doc/whatsnew/3.8.rst
@@ -167,6 +167,16 @@ Added :attr:`SSLContext.post_handshake_auth` to enable and
 post-handshake authentication.
 (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
 ----
 
diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py
index ff85f837d1d5..25f1ff41e4ee 100644
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -3770,6 +3770,24 @@ def selection_element(self, element=None):
         """
         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):
diff --git a/Lib/tkinter/test/test_tkinter/test_widgets.py b/Lib/tkinter/test/test_tkinter/test_widgets.py
index e4c9d337ba7d..c068a9de2d77 100644
--- a/Lib/tkinter/test/test_tkinter/test_widgets.py
+++ b/Lib/tkinter/test/test_tkinter/test_widgets.py
@@ -377,6 +377,31 @@ def test_validatecommand(self):
         self.checkCommandParam(widget, 'validatecommand')
         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)
 class SpinboxTest(EntryTest, unittest.TestCase):
@@ -474,6 +499,31 @@ def test_bbox(self):
         self.assertRaises(TypeError, widget.bbox)
         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)
 class TextTest(AbstractWidgetTest, unittest.TestCase):
diff --git a/Misc/ACKS b/Misc/ACKS
index 272130f4e643..5014584b7bc0 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1094,6 +1094,7 @@ Tim Mitchell
 Zubin Mithra
 Florian Mladitsch
 Doug Moen
+Juliette Monsel
 The Dragon De Monsyne
 Bastien Montagne
 Skip Montanaro
diff --git a/Misc/NEWS.d/next/Library/2018-10-08-16-04-36.bpo-34829.B7v7D0.rst b/Misc/NEWS.d/next/Library/2018-10-08-16-04-36.bpo-34829.B7v7D0.rst
new file mode 100644
index 000000000000..e74b56b83611
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-10-08-16-04-36.bpo-34829.B7v7D0.rst
@@ -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.



More information about the Python-checkins mailing list