Tcl/Tk Cookbook - Using the Canvas


Part-II - Step 1: create a canvas and embed a button widget in it.

The script included in this step generates a user interface that looks like:

The code is self-explanatory. Note that it is useful to assign short variable names to long widget names (e.g., w for .f.sub.c).

#!/usr/bin/wish -f
global countries is1 sc
wm title . "Eu Tool V0.0"
wm minsize . 50 50

frame .f -bg grey -bd 2;# to hold canvas and scrollbars
pack .f
frame .f.sub  -bg red -bd 2 ;# to hold canvas
pack .f.sub -in .f 
canvas .f.sub.c -relief sunken -width 15c -height 15c 
        

pack .f.sub.c -in .f.sub -side left -fill y -padx 2
set w .f.sub.c

frame .info
pack .info
frame .info.sub1
pack .info.sub1 -padx 4 -pady 4 -ipadx 4 -ipady 4
set is1 .info.sub1
label $is1.labcountry -text "Country" -justify right
entry $is1.country   -relief sunken -textvariable sc 

pack $is1.labcountry $is1.country -side left -padx 4 -pady 3 -ipadx 2 -ipady 2
bind $is1.country  {highLightCountry $w $sc}

#
# buttons
#
frame .b -bg grey 
pack .b -fill x

button .b.quit -text Quit -command {exit 0}
button .b.clear -text Clear -command {clearCanvas .f.sub.c}

pack .b.quit .b.clear -side right -padx 4 -pady 4