Version 11 of A small calculator

Updated 2006-11-12 21:53:19

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.

 # Small calculator

  package require Tk

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

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

  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 aufgabe 1}
 #bind . <2> {append aufgabe 2}
 #bind . <3> {append aufgabe 3}
  bind . <4> {append aufgabe 4}
  bind . <r> {append aufgabe 5}
  focus -force .

See also A little calculator


Category Application - Category GUI - Category Office Automation