Example

The following Tcl code demonstrates the use of most of the major widget commands and several of the non-widget commands. The resulting interface is shown in Figure 3-1.

wm withdraw .
set w [toplevel .t]
wm title .t {Tk Code Example}
set m [menu $w.menubar -tearoff 0]
$m add cascade -label File -menu [menu $m.file]
$m.file add command -label Quit -command exit
$m add cascade -label Help -menu [menu $m.help]
$m.help add command -label Index -command {puts Sorry}
$w configure -menu $m

set f [frame $w.f1]
pack [label $f.label -text {A label}] -side left
pack [entry $f.entry] -side left -fill x -expand true
$f.entry insert 0 {This is an entry}
pack $f -fill x -padx 2 -pady 2

set f [frame $w.f2]
pack [frame $f.rg -relief groove -bd 3] -side left -fill x -expand true
pack [label $f.rg.lbl -text Radiobuttons:] -side left
pack [radiobutton $f.rg.b1 -text Tea -variable choice -value 1]
       -side left
pack [radiobutton $f.rg.b2 -text Coffee -variable choice -value 0]
       -side left
pack [frame $f.cg -relief groove -bd 3] -side left -fill x -expand true
pack [label $f.cg.lbl -text Checkbuttons:] -side left
pack [checkbutton $f.cg.b1 -text Cream] -side left
pack [checkbutton $f.cg.b2 -text Sugar] -side left
pack $f -fill x -padx 2 -pady 2

set f [frame $w.f3]
pack [label $f.lbl -text Scale:] -side left
pack [label $f.val -textvariable scaleval -width 4] -side left
pack [scale $f.scl -variable scaleval -orient horizontal -from 0 
       -to 10 -showvalue false] -side left -fill x -expand true
pack $f -fill x -padx 2 -pady 2

set f [frame $w.f4 -relief groove -bd 3]
pack [frame $f.lf] -side left -fill both -padx 3 -pady 3
pack [listbox $f.lf.lb -yscrollcommand "$f.lf.sb set" -height 4] 
    -side left -fill both -expand true
pack [scrollbar $f.lf.sb -command "$f.lf.lb yview"] 
    -side left -fill y
$f.lf.lb insert end {Line 1 of listbox} {Line 2 of listbox}
pack [frame $f.tf] -side left -fill both -expand true -padx 3 -pady 3
grid columnconfigure $f.tf 0 -weight 1
grid rowconfigure $f.tf 0 -weight 1
grid [text $f.tf.tx -yscrollcommand "$f.tf.sy set" -height 4 -width 25 
    -xscrollcommand "$f.tf.sx set"] -column 0 -row 0 -sticky nsew
grid [scrollbar $f.tf.sy -command "$f.tf.tx yview"] 
    -column 1 -row 0 -sticky ns
grid [scrollbar $f.tf.sx -command "$f.tf.tx xview" -orient horizontal] 
    -column 0 -row 1 -sticky ew

$f.tf.tx insert end {This is a text widget}
pack $f -fill both -expand true -padx 2 -pady 2

set f [frame $w.f5]
button $f.b1 -text Apply -default active -command {puts $scaleval}
button $f.b2 -text Reset -default normal -command {set scaleval 0}
button $f.b3 -text Quit -default normal -command exit
pack $f.b1 $f.b2 $f.b3 -padx 10 -side left
pack $f -pady 2
Resulting interface from sample Tk code
Figure 3-1. Resulting interface from sample Tk code
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset