Updated 2016-04-25 20:51:12 by gold

Summary edit

HJG This is aimple demo of Tk: label, entry, button and an image.
A simple layout with frames and pack.
No shortcuts, e.g. combining statements and other tricks.
No error-checks on input (empty, non-numeric) or overflow of result.

Code edit


 #!/bin/sh
 # Restart with tcl: -*- mode: tcl; tab-width: 4; -*- \
 exec wish $0 ${1+"$@"}

 # EntryDemo02.tcl - HaJo Gurt - 2006-02-02 - http://wiki.tcl.tk/15398
 #: Tk-Demo: Enter 3 numbers, show their sum and product

  package require Tk
 
  proc Calc {} {
  #: Calculate results  /  !! No error-checks !!
    global A B C  P S
   #set S [ expr { $A + $B + $C } ]
   #set P [ expr { $A * $B * $C } ]

   # Force calculation as floating point:
    set S [ expr { 0.0 + $A + $B + $C } ]  
    set P [ expr { double($A) * double($B) * double($C) } ]
  }

 #########1#########2#########3#########4#########5#########6#########7#####

  frame  .f1     ;# Frame for input-fields + button
  frame  .f2     ;# Frame for output

  label  .labA -text "A:"
  label  .labB -text "B:"
  label  .labC -text "C:"
  label  .labS -text "S="
  label  .labP -text "P="
 
  entry  .entA -textvar A -width 11
  entry  .entB -textvar B -width 11
  entry  .entC -textvar C -width 11
  entry  .entS -textvar S -state readonly
  entry  .entP -textvar P -state readonly
 
  button .ok -text "OK" -command Calc

  pack   .f1 .f2

  pack   .labA .entA  -in .f1  -side left 
  pack   .labB .entB  -in .f1  -side left
  pack   .labC .entC  -in .f1  -side left
  pack   .ok          -in .f1  -side left -padx 10

  pack   .labS .entS  -in .f2  -side left
  pack   .labP .entP  -in .f2  -side left

 #package require Img                      ;# Images other than .gif
  image create photo Pic1  -file "tcl.gif"
  label  .labPic1  -image Pic1  
  pack   .labPic1  -padx 4 -pady 4

  set A 2147483647   ;# MAXINT
  set B 1
  set C 2
  Calc

  bind . <Return> Calc

  wm title . "EntryDemo2"
  focus -force .entA

Comments edit

Is there a simple/portable way to check for overflow, when working with integer values ?

expr problems with int, 32-bit integer overflow and Arbitrary Precision Math Procedures have some background, but I don't see a nice solution that fits into this program intended to be shown to beginners...

Lars H: The following, taken from Proper integers for Tcl 8.5 or 9.0, is probably the shortest way to check for addition overflow:
  set sum_overflow [expr { $A+$B >> 1  !=  ($A>>1) + ($B>>1) + ($A&$B&1) }]

For multiplication it is probably easiest to do the floating point operation and check the result magnitude, but if you feel a need to have a method completely robust against overflow then sum up the logarithms instead. Luckily this overflow problem will be gone in Tcl 8.5.

For pedagogical purposes, a better solution may be to pick something other than the product of three numbers as the function to demonstrate.
How about doing some date&time calculations, to let clock shine?

HJG: A demo with date&time calculations is a good idea, but surely a step up from working with 'simple numbers'.
I think it is better to cover the basics first.

Screenshot

links to pics have expired

[<<discussion>>