Tkinter: How to get Label wraplength functionality in Text Box

Guilherme Polo ggpolo at gmail.com
Wed Oct 29 10:10:33 EDT 2008


On 10/29/08, Mudcat <mnations at gmail.com> wrote:
> Sounds like that would work really well. Problem is I can't get it to
>  work.
>
>  ...
>  AttributeError: Text instance has no attribute 'count'
>  ...
>

Yep, it is not there yet.

>  I think my usage is correct. I don't have any params at the moment,
>  but I was just checking the functionality.
>
>  numlines = widget.count()
>
>  According to the Tk 8.5 documentation it's used just like a normal
>  command.
>  WIDGET COMMAND
>     pathName count ?options? index1 index2
>         -chars
>         -displaychars
>         -displayindices
>         -displaylines
>         -indices
>         -lines
>         -xpixels
>         -ypixels
>
>
>  As for the environment, I thought I had everything set up correctly.
>  I've got the latest stable version of Python 2.6 (r26:66721, Oct  2
>  2008, 11:35:03). I'm implementing the TTK wrappers to access Tk 8.5.

Implementing a ttk wrapper won't give you this "count" command, since
this is a command for an already existing widget (the text widget), it
is not part of the ttk widgets. Also, there is already a ttk wrapper
at http://pypi.python.org/pypi/pyttk which is being proposed to be
included in python's stdlib.

>  Although when I check the wrapper I don't see any mods to the Text
>  Box. I also don't see this option in the Tkinter.py file.
>
>  Is there something else I need to add to access this new feature?
>

You would need to wrap it and add it as a method to the Text class in
Tkinter. Fortunately it is easily done:

import Tkinter

def text_count(self, index1, index2, *options):
    args = ["-%s" % opt for opt in options]
    args.extend([index1, index2])
    return self.tk.call(self._w, "count", *args)

Tkinter.Text.count = text_count


Then to try it:


root = Tkinter.Tk()
text = Tkinter.Text()
text.pack()

text.insert("1.0", "a\nb\c\nd")
print text.count("1.0", "end", "displaylines", "lines")

root.mainloop()


Note that I inverted the order of the arguments here, indices and then
the options or no options. If it doesn't work saying "count" is not an
acceptable command then your tkinter is not compiled against tcl/tk
8.5 or later.

>
>
>  On Oct 28, 6:51 pm, "Guilherme Polo" <ggp... at gmail.com> wrote:
>
>  >
>  > Are you looking for something like the new "count" command for the
>  > text widget in tk 8.5 ? "count" can count the number of logical lines
>  > (irrespective of wrapping), display lines (counts one for each time a
>  > line wraps) and some other things.
>  >
>
> > >  Thanks
>  >
>  > > --
>  > >  http://mail.python.org/mailman/listinfo/python-list
>  >
>
> > --
>  > -- Guilherme H. Polo Goncalves
>
>
>  --
>  http://mail.python.org/mailman/listinfo/python-list
>


-- 
-- Guilherme H. Polo Goncalves



More information about the Python-list mailing list