Tkinter - incremental input ?

H

Helmut Jarausch

Hi,

I don't want to reinvent the wheel but I cannot find it so far.

Many editors have a so-called incremental search feature.
As you type characters, elements of a set of strings which fit so far are
displayed or at least, the first one of these is displayed.

Now I want to do something similar in Tkinter - an Entry widget
which displays possible 'completions'
e.g. given the list of names (...,'Hardy','Helmut',..)

As soon as enter the character 'H' the string 'Hardy'
would be displayed in the Entry widget - but the cursor
is still at position 2 (given 'H' is a position 1)
Furthermore, as soon as I enter 'e', it would change the
text to 'Helmut', and so on.

While I can bind '<Key>' to a callback, I haven't figured out how
to get (and later on set) the cursor within the Entry widget.
In other words I need to know at which character position the last
character was entered.
Currently I can only see the brute force method: keeping track of
all cursor positioning means like <Backspace>, <Del>, the '<-' and '->' keys
and mouse clicks.
Is there an easier method?

Many thanks for a hint or even a pointer to an example,
Helmut.

--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
E

Eric Brunel

On Wed, 30 Jan 2008 13:32:00 +0100, Helmut Jarausch
While I can bind '<Key>' to a callback, I haven't figured out how
to get (and later on set) the cursor within the Entry widget.
In other words I need to know at which character position the last
character was entered.

You can get the position of the insertion point with
entry.index('insert'), and set it via entry.icursor(index). If you want to
do this as the user types, take care to bind to KeyRelease; this way, the
character corresponding to the key has already been entered in the entry.

BTW, you may also want to automatically select the part that the user
hasn't actually typed. This can be done with entry.selection_clear(), then
entry.selection_range(start_position, end_position).

HTH
 

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,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top