Updated 2016-02-01 00:10:24 by HJG

Summary edit

EKB I needed to have a properly editable table cell in TkTable.
But I still wanted the arrow keys to work for table navigation under normal circumstances.

I added some bindings so that either a double mouse click or pressing ctrl-enter will pop up an edit widget in the active cell.

(If you don't need the arrow keys for table navigation, then the Simple Tktable widget might be what you want.)

Code edit

Here is the basic code:
 event add <<tableEditCell>> <Double-Button-1>
 event add <<tableEditCell>> <Control-Return>

 bind $table.t <<tableEditCell>> {
  # Make sure the cell is up-to-date, first
  forceupdate %W
  # Create an edit widget
  if [winfo exists %W.entry] {
    saveEntry %W
    destroy %W.entry
  set row [%W index active row]
  set col [%W index active col]
  entry %W.entry -validate key -vcmd "validate %W %%P"

  bind %W.entry <KeyPress-Escape> {killEntry %W}
  bind %W.entry <KeyPress-Return> {saveEntry %W; killEntry %W; forceupdate %W}
  bind %W.entry <FocusOut>        {saveEntry %W; killEntry %W; forceupdate %W}

  %W window configure "$row,$col" -window %W.entry -sticky news
  %W.entry insert end [%W get $row,$col]
  %W.entry selection range 0 end

  focus %W.entry

# To work, it needs some application-specific procs (w is the table widget):

 proc forceupdate {w} {
   # ... Update any data structure with the cell contents

 proc killEntry {w} {
   # ... Get rid of the entry widget

 proc validate {w s} {
   # ... Pass the validate command to the entry widget

 proc saveEntry{w} {
   # ... Take the contents of the entry widget and store in the table cell

Comments edit

(Why did I make some of the proc names camel case and some not? Sigh... I try to be consistent, but often miss.)

Why do you need an entry for tktable? Why not just use the built-in cells?

AMG: Eric wanted to have the cells editable only after a special binding triggers.

EKB Thanks AMG! Yes, and also because TkTable makes it hard to edit, since the arrow key bindings are set to navigate the table.

WHD Is there any way to make this check whether the current cell's state is "normal" or "disabled", and edit only if it is "normal"? This is really a question about the Tktable widget; a cell's state derives from the tags it has, and I can't even see any easy way to determine what tags a cell has.