On Thu, 02 Oct 2008 21:57:01 +0200, kib2 <[EMAIL PROTECTED]> wrote:
Hi,

In a tkinter TextWidget I would like to retrieve the last typed word.

I've tried this with the 'wordstart' Expression [From the effbot site, "wordstart" and "wordend" moves the index to the beginning (end) of the current word. Words are sequences of letters, digits, and underline, or single non-space characters.], but it fails :

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from Tkinter import *
root = Tk()
text = Text(root, font=("Calibri"))
text.pack()

# Insert some text inside (13 chars long)
text.insert(INSERT, "one two three")

# idx_st : the position of the cursor
# idx_ed : same but translated to the beginning of the last word ?
idx_st = text.index( "insert" )         # returns 1.13
idx_ed = text.index( "insert wordstart" ) # returns 1.13 too : why ?

Tk/Tkinter apparently considers the position 1.13 to be after the last word in the text. You get the same problem if you set the insertion point just after the word 'two' for example: text.index('insert') returns 1.7 and text.index('insert wordstart') returns 1.7 too...

My solution would simply be to go back one character before asking the word start: text.index('insert - 1 chars wordstart') Don't know if that'll do what you want if there are spaces at the end of the text. And BTW, if you actually want the *last* word in the text, you may want to use text.index('end - 2 chars wordstart'). Here, you have to use '- 2 chars' because end points to the first non-existent index (2.0 in your case...).

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in 'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to