Only in tcl...

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

AMG: Past updates removed the need for [trace], then I updated to not even use variables!

Here it is:

package require Tk

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

bind . <Key> {
  if {[string is print -strict %A]} {
    wm title . [regsub {_$} [wm title .] {}]%A
  } elseif {"%K" eq "BackSpace"} {
    wm title . [string range [regsub {_$} [wm title .] {}] 0 end-1]
  } elseif {"%K" in {Enter Return}} {
    calc
  }
}

proc calc {} {
  if {[wm title .] eq "exit"} {
    bind . <Key> {}
    wm title . Bye...
    after 1000 {destroy .}
  } else {
    catch {wm title . [expr [regsub {_$} [wm title .] {}]]}
  }
}

proc blink {} {
  if {[string index [wm title .] end] eq "_"} {
    wm title . [string range [wm title .] 0 end-1]
  } else {
    wm title . [wm title .]_
  }
  after 250 blink
}

wm title . _
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, ... FB: try replacing the space with a non-breaking space (ASCII 160):

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

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 needed. 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 that don't know about apply yet. I also changed the ::caret to a single space. The numbers jumped with a caret "_" and "".

iu2 Nice to see this program shrinks... Any more ideas?

AMG: Rewrote to not have any variables, global or local. Everything is stored in the window title. The drawback is this makes it impossible to type underscore, but this isn't a big deal for a calculator program.


gold 23Nov2017, added pix with test cosmetics and some categories.

    set tclprecision 17
    .lbl configure -bg palegreen
    .lbl configure -font {fixed 20 bold}
    . configure -background aquamarine4 -highlightcolor brown -relief raised -border 30

only in tcl windows title calculator