tkinter textwidget problem

K

kib2

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 ?

print idx_st,idx_ed
print "Text=",text.get(idx_st,idx_ed)

mainloop()

Any idea ? Thanks in advance :

Kib².
 
E

Eric Brunel

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
 
K

kib2

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...

Exactly.

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

Good idea, it seems to work fine now.
See you.

Kib².
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top