Version 1 of Only in tcl...

Updated 2009-03-24 20:37:30 by iu

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 {%k == 13} 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

enter categories here