Overwriting an entry char by char

ulis, 2002-07-21: the goal is to overwrite the chars of an entry, one by one.


The proc

  # creating the entry widget
  pack [entry .e]
  # focusing the entry widget
  focus -force .e
  # binding to key press
  bind .e <KeyPress> { overwrite %A }
  # overwriting proc
  proc overwrite {ch} \
  {
    if {![string is control $ch]} \
    { # for a standard char

      # updating ::n if needed
      set i [.e index insert]
      if {$i != $::n} { set ::n $i }
      # overwriting the char
      .e delete $::n
      .e insert $::n $ch
      # selecting the next char
      prepare
      # avoiding the Entry class proc
      return -code break
    }
  }
  # next selecting proc
  proc prepare {} \
  {
    incr ::n
    .e icursor $::n
    .e selection from $::n
    .e selection to [expr {$::n + 1}]
  }

The test

  # initial string
  .e insert 0 "the initial string"
  # selecting the first char
  set ::n -1
  prepare

Now, type ahead!


Category Example | Category Widget