9 Type the following scripts, save each to a separate file and run it in wish. Make sure you understand them.

9 Type the following scripts, save each to a separate file and run it in wish. Make sure you understand them. Script 1 # make the main window black . config –bg black #create and pack two frames frame .one –width 5c –height 5c –bg white frame .two –width 10c –height 10c –bg red pack .one .two –side top Script 2 frame .one -bg white frame .two -width 10c -height 10c -bg grey50 foreach b {alpha beta gamma} { button .one.$b -text "$b" pack .one.$b -side left } pack .one .two -side top Script 3 frame .o -width 5c -height 5c frame .t -width 5c -height 5c bind Frame {%W config -bg red} bind Frame {%W config -bg white} bind .t {puts "Button %b at %x %y"} pack .o .t -side left bind all {destroy %W} bind all {focus %W} Script 4 entry .e -width 30 -bg white -relief sunken label .l -text "entry" button .b -text "Display Entry" \ -command {.l config -text [.e get]} pack .e .l .b -side top Script 5 proc MaxLineLength { file } { set max 0 if [catch {open $file} in] { return $in } foreach line [split [read $in] \n] { set len [string length $line] if {$len >$max} { set max $len } } return "Longest line is $max characters" } . config -borderwidth 10 entry .e -width 30 -bg white -relief sunken button .doit -text "Max Line Length " \ -command {.label config -text [MaxLineLength [.e get]]} label .label -text "Enter file name" pack .e .doit .label -side top -pady 5 Script 6 # A frame is first created to hold the menu bar, then it is packed with #another frame below it. Each menu button command has a text argument and a #menu argument that identifies the menu that will be associated with it. frame .mb -relief raised -bd 2 frame .dummy -height 2c pack .mb .dummy -side top menubutton .mb.starters \ -text Starters -underline 0 -menu .mb.m1 menubutton .mb.maincourse \ -text "Main course" -underline 0 -menu .mb.maincourse.m1 menubutton .mb.sweet \ -text Sweets -underline 1 -menu .mb.m3 frame .mb.filler -width 2c pack .mb.starters .mb.maincourse .mb.sweet .mb.filler -side left #Construct menu contents by adding items The command item has a command #argument to define the script that will be executed when this item is #selected. The cascading menu item has a menu option to define the menu as a #child of the current menu. set m [menu .mb.maincourse.m1] $m add command -label "Roast of the day" \ -command {set main roast} $m add cascade -label Steak -menu $m.sub1 $m add cascade -label Salad -menu $m.sub2 set m2 [menu $m.sub1] set m3 [menu $m.sub2] #Populating one of the cascading menus $m2 add command -label Sirloin \ -command {set main "sirloin steak"} $m2 add command -label Rump \ -command {set main "rump steak"} $m2 add command -label Fillet \ -command {set main "fillet steak"} $m2 add command -label T-Bone \ -command {set main "T-bone steak"} 10 Follow the example of the first 4 lines of code in 8, create a topl

Nhận xét

Bài đăng phổ biến