Version 7 of Only in tcl...

Updated 2009-03-27 18:39:49 by AMucha

iu2 2009-03-24 This is a calculator application. So, What's the big deal? The way the window's caption is used...

I apologize, this is very silly, but at the same time a bit challenging, I think. I don't think this could be more elegant in other languages (of the few I know...) thanks to after, trace and binding

Here it is:

  package require Tk

  label .lbl -text "Type a calculation in this odd calculator and press ENTER.\nType 'exit' to exit"
  pack .lbl

  set ::exp "" 
  set ::caret _ 

  bind . <Key> {
    if {[string is print "%A"]} {
      append ::exp %A
    } elseif {"%K" eq "BackSpace"} {
      set ::exp [string range $::exp 0 end-1]
    } elseif {[regexp {Enter|Return} "%K"]} calc
  }

  proc calc {} {
    if {![catch {expr $::exp} res]} {
      set ::exp $res
    } elseif {$::exp eq "exit"} {
      set ::exp Bye...
      after 1000 {destroy .}
    }
  }

  proc blink {} {
    if {$::caret eq "_"} {set ::caret " "} {set ::caret "_"}
    wm title . $::exp$::caret
    after 250 blink
  }

  # trace add variable ::exp write {apply {{name1 name2 op} {wm title . $::exp}}} 
  raise .
  after 250 blink

Googie 24 Mar 2009 - It's fan! I imagine application where we go to settings dialog and there is option like Edit window title inline :) The only ugly behaviour is in case like I have window manager configured to display title in the center of top bar, so blinking cursor causes whole title to be moved a little left and right, left and right, ...

slebetman - The problem is, on my machine (Ubuntu AMD64) the %k value for the Enter key is 36 and for keypad Enter its 104. I replaced the elseif {%k == 13} part in bind .<Key> with elseif {[regexp {Enter|Return} "%K"]}. Hope you don't mind.

AMucha The trace is not really neded. There is an update every 250 ms. Most people can wait that long. Another benefit: The program will work with pre 8.5 Tcl/Tk Versions wich don't know about apply jet. I also changed the ::caret to a single space. The numbers jumped with a caret "_" and "".


enter categories here