aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/tkinter
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2024-02-11 12:43:14 +0200
committerGitHub <noreply@github.com>2024-02-11 12:43:14 +0200
commitd2c4baa41ff93cd5695c201d40e20a88458ecc26 (patch)
tree9cd066f17f0aabe268312d469f94447a4063d592 /Lib/tkinter
parent5d2794a16bc1639e6053300c08a78d60526aadf2 (diff)
downloadcpython-d2c4baa41ff93cd5695c201d40e20a88458ecc26.tar.gz
cpython-d2c4baa41ff93cd5695c201d40e20a88458ecc26.zip
gh-97928: Partially restore the behavior of tkinter.Text.count() by default (GH-115031)
By default, it preserves an inconsistent behavior of older Python versions: packs the count into a 1-tuple if only one or none options are specified (including 'update'), returns None instead of 0. Except that setting wantobjects to 0 no longer affects the result. Add a new parameter return_ints: specifying return_ints=True makes Text.count() always returning the single count as an integer instead of a 1-tuple or None.
Diffstat (limited to 'Lib/tkinter')
-rw-r--r--Lib/tkinter/__init__.py23
1 files changed, 15 insertions, 8 deletions
diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py
index 2be9da2cfb9..175bfbd7d91 100644
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -3745,7 +3745,7 @@ class Text(Widget, XView, YView):
return self.tk.getboolean(self.tk.call(
self._w, 'compare', index1, op, index2))
- def count(self, index1, index2, *options): # new in Tk 8.5
+ def count(self, index1, index2, *options, return_ints=False): # new in Tk 8.5
"""Counts the number of relevant things between the two indices.
If INDEX1 is after INDEX2, the result will be a negative number
@@ -3753,19 +3753,26 @@ class Text(Widget, XView, YView):
The actual items which are counted depends on the options given.
The result is a tuple of integers, one for the result of each
- counting option given, if more than one option is specified,
- otherwise it is an integer. Valid counting options are "chars",
- "displaychars", "displayindices", "displaylines", "indices",
- "lines", "xpixels" and "ypixels". The default value, if no
- option is specified, is "indices". There is an additional possible
- option "update", which if given then all subsequent options ensure
- that any possible out of date information is recalculated."""
+ counting option given, if more than one option is specified or
+ return_ints is false (default), otherwise it is an integer.
+ Valid counting options are "chars", "displaychars",
+ "displayindices", "displaylines", "indices", "lines", "xpixels"
+ and "ypixels". The default value, if no option is specified, is
+ "indices". There is an additional possible option "update",
+ which if given then all subsequent options ensure that any
+ possible out of date information is recalculated.
+ """
options = ['-%s' % arg for arg in options]
res = self.tk.call(self._w, 'count', *options, index1, index2)
if not isinstance(res, int):
res = self._getints(res)
if len(res) == 1:
res, = res
+ if not return_ints:
+ if not res:
+ res = None
+ elif len(options) <= 1:
+ res = (res,)
return res
def debug(self, boolean=None):