Linux Penguin Guide

Linux Programming

  1. Tcl
  2. Tk+


I. Tcl (Tool Command Language) and Tk

a. Programming Tcl

Definition of Tcl : - A simple, interpreted language

Type in the first Tk program, that displays a button, which on pressed, exits :

xbutton.tk [Kamran]

# Push-Button which Exits

button .b -text "Hello World" -command exit
pack .b

It is executed via $ wish xbutton.tk (wish = `Windows Shell')

b. Features of Tk

xradiobutton.tk [Kamran]

# Radiobutton and Checkbutton Demo

radiobutton .r -text "Fish" -command exit
checkbutton .c -text "Crab" -command exit
pack .r
pack .c

xmenubutton.tk [Running.350]

frame .fr -relief groove -bd 3
menubutton .fr.mfile -text "File"
menubutton .fr.mobj -text "Object"
pack .fr -side top -expand yes -fill x
pack .fr.mfile .fr.mobj -side left

xmenu.tk [Running.350]

frame .fr -relief groove -bd 3
menubutton .fr.mfile -text "File" -menu .fr.mfile.menu
menubutton .fr.mobj -text "Object" -menu .fr.mobj.menu

pack .fr -side top -expand yes -fill x
pack .fr.mfile .fr.mobj -side left

menu .fr.mfile.menu
.fr.mfile.menu add command -label "Quit" -command { exit }

menu .fr.mobj.menu
.fr.mobj.menu add radiobutton -label "Ovals" -command { exit }
.fr.mobj.menu add radiobutton -label "Rectangle" -command { exit }

The following program draws shapes to the canvas :

shapes.tk [Running.347f]

frame .fr -relief groove -bd 3

set oval_count 0
set rect_count 0
set orig_x 0
set orig_y 0

proc set_oval {} {
  global oval_count orig_x orig_y

# bind event to pressing mouse button 1 in the region .c ie. canvas
  bind .c  {
   set orig_x %x
   set orig_y %y
   set oval_count [expr $oval_count + 1]
   .c create oval %x %y %x %y -tags "oval$oval_count" -fill red
  }

    bind .c  {
  .c delete "oval$oval_count"
  .c create oval $orig_x $orig_y %x %y -tags "oval$oval_count" -fill red
  }  
 }

proc set_rect {} {
  global orig_x orig_y
# bind event to pressing mouse button 1 in the region .c ie. canvas
  bind .c  {
   set orig_x %x
   set orig_y %y
   set rect_count [expr $rect_count + 1]
   .c create rectangle $orig_x $orig_y %x %y -tags "rec$rect_count" -fill blue
  }

   bind .c  {
  .c delete "rect$rect_count"
  .c create rectangle $orig_x $orig_y %x %y -tags "rect$rect_count" -fill blue
  }  
}

menubutton .fr.mfile -text "File" -menu .fr.mfile.menu
menubutton .fr.mobj -text "Object" -menu .fr.mobj.menu 

pack .fr -side top -expand yes -fill x
pack .fr.mfile .fr.mobj -side left

# create a menu
menu .fr.mfile.menu
.fr.mfile.menu add command -label "Quit" -command { exit }

canvas .c
pack .c -side top

menu .fr.mobj.menu
.fr.mobj.menu add radiobutton -label "Ovals" -command { set_oval }
.fr.mobj.menu add radiobutton -label "Rectangle" -command { set_rect }

xmenubutton.tk [Running.350]

frame .fr -relief groove -bd 3
menubutton .fr.mfile -text "File" -menu .fr.mfile.menu
menubutton .fr.mobj -text "Object" -menu .fr.mobj.menu

pack .fr -side top -expand yes -fill x
pack .fr.mfile .fr.mobj -side left

menu .fr.mfile.menu
.fr.mfile.menu add command -label "Quit" -command { exit }

menu .fr.mobj.menu
.fr.mobj.menu add radiobutton -label "Ovals" -variable objtype \
  -command { exit }

xentry.tk [Running.343]

# Run with $wish FileEdit.tk

# Create label widget .l and entry widget .e

label .l -text "Filename : "
entry .e -relief sunken -width 30 -textvariable fname

# Place the widgets into the application window
pack .l -side left
pack .e -side left -padx 1m -pady 1m

# When return key is pressed, run xterm
bind .e  {
                  exec xterm -e vi $fname 
                 }

III. Programming Languages

a. Compilers

b. Mathematical Aids



References