Tkinter: How to get Label wraplength functionality in Text Box

Mudcat mnations at gmail.com
Thu Oct 30 09:43:26 EDT 2008


I'm not sure why my tkinter would not be compiled against 8.5 since I
have the latest version. I assumed that Python 2.6 would have it
without requiring me to do an extra compile.

However I was able to get it working using the code you gave me.
Thanks for that. The only problem is that it seems to simply be
counting newlines (or number of \n). When I use the following:

        numlines = widget.count("1.0", "end", "displaylines", "lines")
        print "Number of lines is ", numlines

I get this:

Number of lines is  (153, 1)

So that's not actually the number of lines displayed in the box, just
the number of newline chars it finds. I couldn't find anything in the
tk documentation that would give me any other options to count lines
differently, or number of lines displayed after wrapping.


On Oct 29, 9:10 am, "Guilherme Polo" <ggp... at gmail.com> wrote:

>
> You would need to wrap it and add it as a method to the Text class inTkinter. Fortunately it is easily done:
>
> importTkinter
>
> 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 yourtkinteris not compiled against tcl/tk
> 8.5 or later.
>
>
>
>



More information about the Python-list mailing list