I'm writing some GUI program in Tkinter. I'm trying to trace the contents of the Text widget by using Text.get() method.
I heard that I can get(1.0, 'end') to get current contents written on the widget, but it doesn't give the most recent character I typed.
This is a simple example I wrote:
import Tkinter as tk
class TestApp(tk.Text, object):
def __init__(self, parent = None, *args, **kwargs):
self.parent = parent
super(TestApp, self).__init__(parent, *args, **kwargs)
self.bind('<Key>', self.print_contents)
def print_contents(self, key):
contents = self.get(1.0, 'end')
print '[%s]' % contents.replace('\n', '\\n')
if __name__ == '__main__':
root = tk.Tk()
app = TestApp(root, width = 20, height = 5)
app.pack()
root.mainloop()
If I type 'abcd', it prints 'abc\n', not 'abcd\n'. ('\n' is automatically added after the last line by the Text widget.)
How can I get 'abcd\n', instead of 'abc\n'?
[Solved]
Thanks to Bryan Oakley and Yasser Elsayed, I solved the problem by replacing
self.bind('<Key>', self.print_contents)
to the following:
self.bind('<KeyRelease>', self.print_contents)