Skip to content

tkinter.Text.count(index1, index2) returns None not (0,) when index1 equals index2 #97928

Closed
@RogerMarsh

Description

@RogerMarsh

Surely the Text.count() method should return (0,) in the title code snippet? Following the example of what happens when more than one option is given.

I suppose the text_count method should do so too, but it was a hack of something that did not exist when written.

Tk8.6 documentation says a list of integers is returned.

Sample code below.

import tkinter


def text_count(widget, index1, index2, *options):
    """Hack Text count command. Return integer, or tuple if len(options) > 1.

    Tkinter does not provide a wrapper for the Tk Text widget count command
    at Python 2.7.1

    widget is a Tkinter Text widget.
    index1 and index2 are Indicies as specified in TkCmd documentation.
    options must be a tuple of zero or more option values.  If no options
    are given the Tk default option is used.  If less than two options are
    given an integer is returned.  Otherwise a tuple of integers is returned
    (in the order specified in TkCmd documentation).

    See text manual page in TkCmd documentation for valid option values and
    index specification.

    Example:
    chars, lines = text_count(widget, start, end, '-chars', '-lines')

    """
    return widget.tk.call((widget._w, 'count') + options + (index1, index2))


text = tkinter.Text()

print(text.count("1.0", tkinter.END))                                       # (1,0)
print(text.count("1.0", "1.0"))                                                 # None
print(text_count(text, "1.0", "1.0"))                                        # 0
print(text_count(text, "1.0", tkinter.END))                              # 1
print(text.count("1.0", tkinter.END, "chars"))                         # (1,)
print(text.count("1.0", "1.0", "chars"))                                   # None
print(text_count(text, "1.0", "1.0", "-chars"))                         # 0
print(text_count(text, "1.0", tkinter.END, "-chars"))               # 1
print(text.count(tkinter.END, "1.0", "chars"))                         # (-1,)
print(text_count(text, tkinter.END, "1.0", "-chars"))               # -1
print(text.count("1.0", tkinter.END, "chars", "lines"))             # (1, 1)
print(text.count("1.0", "1.0", "chars", "lines"))                       # (0, 0)
print(text_count(text, "1.0", "1.0", "-chars", "-lines"))            # (0, 0)
print(text_count(text, "1.0", tkinter.END, "-chars", "-lines"))  # (1, 1)

Linked PRs

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions