Tcl/Tk Cookbook - Canvas Revisited


Step 1: Create basic graphical user interface.

Script

#!/usr/local/bin/wish -f  ;#start wish and pass this script for parsing.


global sb so x1 y1 lw  ;#declare some globals

frame .fr -width 24c -height 13.6c -bd 2 ;#main frame
pack .fr

wm title . "Canvas"

frame .fr.menubar -relief raised -bd 2
pack .fr.menubar -padx 1 -fill x

frame .fr.panl -width 3.6c -height 12.8c -bg black
frame .cfr -width 20.4c -height 12.4c -bd 1

pack .fr.panl .cfr -in .fr -side left -padx 2 -after .fr.menubar -fill x

canvas .can -width 20c -height 12.0c -bg grey -xscrollcommand ".xs set" \
	-yscrollcommand ".ys set"
scrollbar .ys -command ".can yview" 
pack .can .ys -in .cfr -side left -fill x -fill y
scrollbar .xs -orient horizontal -command ".can xview"
place .xs -in .fr -x 3.8c -y 13.3c -width 20.2c


#fill the  top menu
menubutton .fr.menubar.file -text File -underline 0 -menu .fr.menubar.file.menu
menubutton .fr.menubar.edit -text Edit -underline 0 -menu .fr.menubar.edit.menu
menubutton .fr.menubar.graphics -text Graphics -underline 0 -menu \
		.fr.menubar.graphics.menu

pack .fr.menubar.file .fr.menubar.edit .fr.menubar.graphics -side left


menubutton .fr.menubar.help -text Help -underline 0 
pack .fr.menubar.help -side right

#File menu
menu .fr.menubar.file.menu
.fr.menubar.file.menu add command -label Print -command {printCanvas}
.fr.menubar.file.menu add command -label Quit -command exit

#Edit menu

menu .fr.menubar.edit.menu
.fr.menubar.edit.menu add command -label Cut -com {CutSelection}
.fr.menubar.edit.menu add command -label Clear -com {clearCanvas}

#Graphics menu

menu .fr.menubar.graphics.menu
.fr.menubar.graphics.menu add cascade -label "Line Width" \
	-menu .fr.menubar.graphics.menu.fmenu


#Second level menu for Line Width

menu .fr.menubar.graphics.menu.fmenu
.fr.menubar.graphics.menu.fmenu add radiobutton -label "0.5" \
		-com {set lw 0.5}
.fr.menubar.graphics.menu.fmenu add radiobutton -label "2.0" \
	-com { set lw 2.0 }
set lw 1.0

The above script creates a Tk application main window titled "canvas" with a main frame ".fr".

The main frame contains three frame widgets - the frame ".fr.menubar" for the top menubar, the frame ".fr.panl" is placed below the menubar packed to the left of the third frame ".cfr". Note that the height and width of the main frame as well as those of ".fr.panl" and ".cfr" are given in units of centimetres.

Tk canvas widget ".can" is created as the child of the main (application root) window and placed within ".cfr". The scrollbars ".xs" (horizontal) and ".ys" vertical are created and connected to the canvas widget.

the top menubar contains four menubuttons with labels "File", "Edit", "Graphics" and "Help". The menu entries for "File" is Print and Quit with associated actions printCanvas and exit; the menu entries for "Edit" are Cut and Clear with associated actions CutSelection and clearCanvas. "Graphics" supports another level of menu to set line width for the drawing primitives.

Tk place command

Note that for the horizontal scrollbar, the place rather than pack command is used with fixed size and location parameters to position slave widgets within their masters. This allows for the slave to resize itself when the master changes size, preserving relative configuration.

The resultant graphical user interface (gui) would be similar to: