[Tkinter-discuss] About making Text.tag_*range methods more consistent

Michael Lange klappnase at web.de
Mon Jun 9 08:36:25 CEST 2014


Hi,

On Sun, 8 Jun 2014 19:25:44 +0530
Saimadhav Heblikar <saimadhavheblikar at gmail.com> wrote:

> Some code,
> 
> >>> from tkinter import Text
> >>> text = Text()
> >>> text.pack()
> >>> text.insert('insert', 'ab cd ef')
> >>> text.tag_add('red', '1.0','1.2')
> >>> text.tag_add('red', '1.6', '1.8')
> >>> text.tag_prevrange('red', 'end')
> ('1.6', '1.8')
> >>> text.tag_ranges('red')
> (<textindex object: '1.0'>, <textindex object: '1.2'>, <textindex
> object: '1.6'>, <textindex object: '1.8'>)
> >>> text.tag_nextrange('red', '1.0')
> ('1.0', '1.2')
> 
> I propose that Text.tag_ranges(tag) return a list of tuples (start
> index, end index), instead of a list like [start index1, endindex1,
> startindex2, endindex2....etc]
> 
> To be more even more consistent, tag_ranges tuples should contain
> indexes similar to the ones returned by tag_prevrange/tag_nextrange
> i.e. strings instead of textindex objects.

I don't think that these textindex objects are intentional, they might be
worth a bug report. I have encountered similar objects instead of the
expected return values from tk methods at what appears to me as "random"
places from time to time with hevery new version for years since the
Tkinter developers invented the "object layer" (sorry, no idea how they
actually call it) in Python-2.4 or so. In Python2 you can disable
it altogether by setting Tkinter.wantobjects = False early in your code
to restore the "classic" behavior, unfortunately this does not seem to
work in Python3 anymore (at least the last time I tried). Probably these
objects can easily be converted into strings if you know where they
appear.

> 
> So, the above output would have been
> >>> text.tag_ranges('red')
> (('1.0', '1.2'), ('1.6', '1.8'))

I don't know if there is a special reason why they did not use "nested"
tuples, maybe it is simply because in the early development of Tkinter
they tended to keep as close to the original Tk behavior as possible.
Otherwise they might have even turned the index strings, as "1.6" into
tuples; I don't feel this is a big issue, though, once you get used to it.

Best regards

Michael


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

	"It's hard to believe that something which is neither seen nor
felt can do so much harm."
	"That's true.  But an idea can't be seen or felt.  And that's
what kept the Troglytes in the mines all these centuries.  A mistaken
idea."
		-- Vanna and Kirk, "The Cloud Minders", stardate 5819.0


More information about the Tkinter-discuss mailing list