Tcl/Tk Cookbook - Tcl/Tk and FORTRAN


Step 2: Create a Tk front-end for the equation solver

A simple Tk interface to the quadratic eqaution solver should have three entry widgets for the user to input the values of the coefficients, labels to display the values of the roots and the discriminant etc.

The Tk script below creates an interface similar to:

Tk Script starts here

Place the code in your current working directory in a file named "quads.tcl".



#!/usr/bin/wish -f
wm title . "gui_quads"
label .msg -text "Solution of a x^2 + b x + c = 0"
pack .msg -padx 5 -pady 3 -ipadx 5 -ipady 5 -fill x
frame .f
pack .f -padx 5 -ipadx 5

Padding

The options -padx, -pady, -ipadx and -ipady take values specified in terms of number of pixels or units of length (m for millimetre, c for centimetre).

The options -padx and -pady tell the packer to allow the specified space in the horizontal and vertical directions between the slaves when the slaves are packed within a frame. This is known as external padding.

The options -ipadx and -ipady tell the packer to enlarge the slave window in the horizontal and vertical directions by the given value before packing it within its master. This is known as internal padding.

External padding results in space between sibling widgets in a parent. Internal padding enlarges a slave to more than its size calculated by the geometry manager (e.g a label widgetis made larger than the minimum width it requires to display its label string).

Back to Tk Script

Append the following to the last few lines of the script.


entry .f.a   -relief sunken  ;#for coefft. of x**2
label .f.x2  -text "x^2 + "
entry .f.b   -relief sunken  ;#for coefft. of x
label .f.x   -text " x + "
entry .f.c   -relief sunken ;# for constant term
label .f.rhs -text " = 0"

pack .f.a .f.x2 .f.b .f.x .f.c .f.rhs -in .f -side left \
	-padx 3 -pady 3 -ipadx 2 -ipady 2

frame .zeros
pack .zeros
frame .zeros.base1 -bg red
frame .zeros.base2 -bg pink
pack .zeros.base1 -in .zeros -padx 5 -pady 5 -side top
pack .zeros.base2 -in .zeros -padx 5 -pady 5 -side top
label .zeros.x1 -text "x1 = "   

label .zeros.x1val -bg yellow   ;#to display the value of first real root
				;# or real part of the complex roots

label .zeros.x2 -text "x2 = "
label .zeros.x2val -bg yellow	;#to display the value of second real root
				;# or imaginary part of the complex roots

pack .zeros.x1 .zeros.x1val -side left -in .zeros.base1 -padx 5 -pady 5
pack .zeros.x2 .zeros.x2val -side left -in .zeros.base2 -padx 5 -pady 5

# 
frame .info
pack .info
frame .info.dum
pack .info.dum -side left 
set w .info.dum
label $w.disc    ;#label to display the discriminant
label $w.type	 ;#label to display the type of the roots (real or complex)

pack $w.disc -padx 5 -pady 5 
pack $w.type -padx 5 -pady 5
#

foreach e {.f.a .f.b .f.c} {
	 bind $e  {invokeQuads}
}
#

#
# buttons
#
frame .bf
pack .bf -padx 5 -pady 5 -ipadx 4 -ipady 4 -fill x

button .bf.quit -text Quit -command {exit}          ;#exit button to quit 
button .bf.clear -text Clear -command clearEntries  ;#Resets the entry fields
button .bf.solve -text Solve -command invokeQuads   ;#calls "quads"

pack .bf.quit .bf.clear .bf.solve -side right \
     -padx 5 -pady 5 -ipadx 3 -ipady 3

focus .f.a  ;#set keyboard focus into first entry widget


The procedures invokeQuads and clearEntries are described in the next step.