A small calculator

Introduction

This is a small 4-function-calculator. It uses expr directly to calculate the results, so you may need to append ".0" to numbers to do calculations in floating point.

E.g. "3/2" will be done on integers: "3/2=1", so you might want to do "3/2.0" instead.

Also, you cannot use the result of a calculation for further calculations, i.e. if you have "17/4.0=4.25" in the display and you continue to type "*3=" you get an error.


Program 1

  # Small calculator

  package require Tk

  button .quit   -text "Off" -fg red -command exit
  label .readout -textvariable result -bg white
  set result " "
  button .key0  -text "0"    -width 3 -command {append result 0}
  button .key1  -text "1"    -width 3 -command {append result 1}
  button .key2  -text "2"    -width 3 -command {append result 2}
  button .key3  -text "3"    -width 3 -command {append result 3}
  button .key4  -text "4"    -width 3 -command {append result 4}
  button .key5  -text "5"    -width 3 -command {append result 5}
  button .key6  -text "6"    -width 3 -command {append result 6}
  button .key7  -text "7"    -width 3 -command {append result 7}
  button .key8  -text "8"    -width 3 -command {append result 8}
  button .key9  -text "9"    -width 3 -command {append result 9}

  button .point -text "."    -width 3 -command {append result .}
  button .plus  -text "+"    -width 3 -command {append result +}
  button .minus -text "-"    -width 3 -command {append result -}
  button .times -text "*"    -width 3 -command {append result *}
  button .div   -text "/"    -width 3 -command {append result /}
  button .equal -text "="    -width 3 -command {append result = [expr $result]}
  button .sign  -text "+/-"  -width 3 -command {set result [expr $result*-1]}
  button .clear -text "C/CE" -width 3 -command {set result ""}

  grid .quit .readout -sticky nsew
  grid .key7 .key8 .key9 .times .clear -sticky nsew
  grid .key4 .key5 .key6 .minus .div   -sticky nsew
  grid .key1 .key2 .key3 .plus  .equal -sticky nsew
  grid .key0 .sign .point              -sticky nsew

 #More than 4 columns:
 #grid configure .sbar    -columnspan 4 -padx 4
  grid configure .readout -columnspan 4 -padx 4
  grid configure .plus    -rowspan 2
  grid configure .equal   -rowspan 2

 #bind . <1> {append result 1}
 #bind . <2> {append result 2}
 #bind . <3> {append result 3}
  bind . <4> {append result 4}
  bind . <r> {append result 5}
  focus -force .

HJG 2006-11-12 - Changed "aufgabe" --> "result".

HJG 2014-03-28 - Here is a slightly revised version: Extending the "clear"-button, we can re-use the result for further calculations:

  • if the result contains a "=", the first part of the text is removed,
    i.e "17/4.0=4.25" --> "4.25", ready for more operations.
  • otherwise, the last char is removed ("backspace")

Likewise, the "+/-"-button got an extension:

  • if the result contains a "=", the text from there to end of text is removed, and ".0" appended,
    i.e "17/4=4" --> "17/4.0", ready to retry on real numbers.

Also, I wrote the button-definition a bit shorter.

Program 2

  # Small calculator v2

  package require Tk

  global result 
  set result " "
  
  proc Sign {} {    
    set p [string first "=" $::result]
    if {$p>=0} {
        set ::result [string range $::result 0 $p-1]  
        append ::result ".0"
    } else {
        set ::result [expr $::result*-1]
    }
  }
  proc C_CE {} {
    set p [string first "=" $::result]
    if {$p>=0} {    ;# clear result: remove text upto "="
        incr p 1
        set ::result [string range $::result $p end] 
    } else {        ;# Backspace: remove last char
        set ::result [string range $::result 0 end-1] 
    }
  }

  button .quit   -text "Off" -fg red -command exit
  label .readout -textvariable result -bg white

  foreach k { 0 1 2 3 4 5 6 7 8 9   + - * / } {
    button .key$k -text "$k" -width 3 -command "append result $k"
  } 
  button .point -text "."    -width 3 -command {append result .}
  
  button .equal -text "="    -width 3 -command { append result = [expr $result]}
  button .sign  -text "+/-"  -width 3 -command { Sign }
  button .clear -text "C/CE" -width 3 -command { C_CE }

  grid .quit .readout -sticky nsew
  grid .key7 .key8 .key9 .key* .clear -sticky nsew
  grid .key4 .key5 .key6 .key- .key/  -sticky nsew
  grid .key1 .key2 .key3 .key+ .equal -sticky nsew
  grid .key0 .sign .point             -sticky nsew

  grid configure .readout -columnspan 4 -padx 4
  grid configure .key+    -rowspan 2
  grid configure .equal   -rowspan 2

  focus -force .

A small calculator screen.png

gold added pix


See also: A little calculator - A fancier little calculator