Don't get all the text and then count, just get the index of the last-minus-one character with index("end-1c")
and then do some string manipulation to get the line number.
As for why the number doesn't increase, it's because your binding happens before the return key is inserted. For your simple test you can work around this by binding on <KeyRelease>
, since the character is inserted on the press.
import Tkinter as Tk
def countlines(event):
(line, c) = map(int, event.widget.index("end-1c").split("."))
print line, c
root = Tk.Tk()
root.geometry("200x200")
a = Tk.Text(root)
a.pack()
a.bind("<KeyRelease>", countlines)
root.mainloop()
If you need to print the value on the key press, you'll have to use an advanced feature called "bindtags". Bindtags are covered briefly in an answer to this question: Basic query regarding bindtags in tkinter. In short, you have to create a custom bindtag that appears after the class bindtag, so that your binding happens after the class binding.
Here's how to modify your program to use bindtags:
import Tkinter as Tk
def countlines(event):
(line, c) = map(int, event.widget.index("end-1c").split("."))
print line, c
root = Tk.Tk()
root.geometry("200x200")
a = Tk.Text(root)
a.pack()
bindtags = list(a.bindtags())
bindtags.insert(2, "custom")
a.bindtags(tuple(bindtags))
a.bind_class("custom", "<Key>", countlines)
root.mainloop()