tk widgets this material is best on several sources –slides by dr. ernest j. friedman-hill...
TRANSCRIPT
![Page 1: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/1.jpg)
Tk Widgets
This material is best on several sources
– Slides by Dr. Ernest J. Friedman-Hill
– various Tcl/Tk books
![Page 2: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/2.jpg)
Building User Interfaces With Tk
You build interfaces with
– Widget commands
buttons, labels, list boxes, text widgets, canvases, etc.
often includes callbacks
– Geometry management
widget layout tools
place, pack and grid
– Bindings
bind user actions to widgets / objects to invoke commands
– Window Manager commands
to control your window
![Page 3: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/3.jpg)
Widgets implemented by Tk
Frame Menubutton CanvasLabel Menu ScrollbarButton Message ScaleCheckbutton Entry ListboxRadiobutton Text Toplevel
Try the Widget Tour on the PCs to get an overviewStart/Tcl/Widget Tour
![Page 4: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/4.jpg)
The Widget Hierarchy
.
.menu.file
.scroll
.menu.help
.menu.listbox
![Page 5: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/5.jpg)
Creating Widgets
Each widget has a class: button, listbox, scrollbar, etc.
One class command for each class, used to create instances:
button .b -text Quit -command exit
scrollbar .x -orient horizontal
class name
window name
configuration options
![Page 6: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/6.jpg)
Configuration Options Defined by class. For buttons:
-activebackground -disabledforeground -justify -underline
-activeforeground -font -padx -width -anchor -foreground -pady -wraplength -background -height -relief-bitmap -highlightbackground -state-borderwidth -highlightcolor -takefocus-command -highlightthickness -text-cursor -image -textvariable
Default provided by class.button .b -text Hello -command exit
button .c -bitmap @$tk_library/demos/images/flagdown.bmp
grid .b .c
Advanced: may be specified in an options database
![Page 7: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/7.jpg)
Widget Commands
Tcl command for each widget, named after widget.
Used to reconfigure, manipulate widget:button .b -text "Hello".b configure -text Goodbye
Configurations are readable and modifiable, anytime
–very powerful, as can change anything
Widget command is deleted when widget is destroyed.
–destroy .b
![Page 8: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/8.jpg)
Geometry Management
Widgets don't control their own positions and sizes: geometry managers do.
Widgets don't even appear on the screen until managed by a geometry manager.
Geometry manager = algorithm for arranging slave windows relative to a master window.
Constraint-based systemi.e., what to do when widgets / windows change size
GeometryManager
Requested size from slave
Parameters from application designer
Geometry of master
Size and location of slave
Requested size for master
![Page 9: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/9.jpg)
The Placer Simple but not very powerful Each slave placed individually relative to its master. Avoid using it
button .b -text X
place .b -x 0 -y 0
![Page 10: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/10.jpg)
The Grid
Lays out objects in virtual grid of rows and columns powerful, but good for most simple placements
entry .e
.e insert 0 "Type text here"
button .b1 -text "Wow!"
button .b2 -text "Ho Hum"
grid .e -row 0 -column 0 -columnspan 2 -sticky ew
grid .b1 -row 1 -column 0
grid .b2 -row 1 -column 1
![Page 11: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/11.jpg)
The Grid
Another example
label .to_label -text "To:"
entry .to
label .from_label -text "From:"
entry .from
text .t -width 24 -height 10
grid .to_label -row 0 -column 0 -sticky e
grid .to -row 0 -column 1 -sticky ew
grid .from_label -row 1 -column 0 -sticky e
grid .from -row 1 -column 1 -sticky ew
grid .t -row 2 -column 0 -columnspan 2 -sticky nsew
![Page 12: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/12.jpg)
The Packer
More powerful than the placer, but more complex Arranges groups of slaves together (packing list). Packs slaves around edges of master's cavity. For each slave, in order:
1. Pick side of cavity.
2. Slice off parcelfor slave.
4. Position slavein parcel.
3. Optionally grow slaveto fill parcel.
![Page 13: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/13.jpg)
The Packer: Choosing Sides
button .ok -text OKbutton .cancel -text Cancelbutton .help -text Help
pack .ok .cancel .help -side left
.cancel configure -text "Cancel Command"
pack .ok .cancel .help -side top
![Page 14: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/14.jpg)
The Packer: Padding
pack .ok .cancel .help -side left \-padx 2m -pady 1m
pack .ok .cancel .help -side left \-ipadx 2m -ipady 1m
pack .ok .cancel .help -side left \-padx 2m -pady 1m -ipadx 2m -ipady 1m
![Page 15: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/15.jpg)
The Packer: Filling
Stretch widgets to fill parcels:
pack .ok .cancel .help -side top
pack .ok .cancel .help -side top -fill x
![Page 16: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/16.jpg)
The Packer: Filling, cont'd
pack .menu -side toppack .scrollbar -side rightpack .listbox
pack .menu -side top -fill xpack .scrollbar -side right -fill ypack .listbox
![Page 17: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/17.jpg)
The Packer: Expansion
Increase parcel size to absorb extra space in master:
pack .ok .cancel .help -side left
pack .ok .cancel -side leftpack .help -side left \ -expand true -fill x
pack .ok .cancel -side leftpack .help -side left -expand true
![Page 18: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/18.jpg)
The Packer: Expansion, cont'd
pack .ok .cancel .help -side left \ -expand true
pack .ok .cancel .help -side left \ -expand 1 -fill both
![Page 19: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/19.jpg)
Hierarchical Packing
Use additional frames to create more complex arrangements:frame .leftpack .left -side left -padx 3m -pady 3mframe .rightpack .right -side right -padx 3m -pady 3m
foreach size {8 10 12 18 24} { radiobutton .pts$size -variable pts \ -value $size -text "$size points"}pack .pts8 .pts10 .pts12 .pts18 .pts24 \ -in .left -side top -anchor w
checkbutton .bold -text Bold \ -variable boldcheckbutton .italic -text Italic \ -variable italiccheckbutton .underline -text Underline \ -variable underlinepack .bold .italic .underline \ -in .right -side top -anchor w
![Page 20: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/20.jpg)
Callbacks How to make widgets work together with application,
other widgets? Tcl scripts. Widget actions are Tcl commands:
button .a.b -command exit
exitbutton release
![Page 21: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/21.jpg)
Bindings
Associate Tcl scripts with user events:
bind .e <Control-h> {backspace .t}
Window(s) Event Script
![Page 22: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/22.jpg)
Bindings: Specifying Events
Specifying events:
<Double-Control-ButtonPress-1>
<3>
<KeyPress>
a
Modifiers Event Type
Button or Keysym
![Page 23: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/23.jpg)
Bindings: Substitutions
% substitutions in binding scripts:
– Coordinates from event: %x and %y.
– Window: %W.
– Character from event: %A.
– Many more... Examples:
bind .c <B1-Motion> {move %x %y}
bind .t <KeyPress> {insert %A}
![Page 24: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/24.jpg)
Example: Context sensitive help
button .b -text "Hello World" -command exit
button .c -text "Lazy Boy"
label .help
pack .b .c .help -side top
bind .b <Enter> {.help configure -text "Press to exit"}
bind .c <Enter> {.help configure -text "i do nothing"}
bind Button <Leave> {.help configure -text ""}
![Page 25: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/25.jpg)
Access To Other Facilities
Keyboard focus:focus .x.y
Communication with window manager:wm title . "Editing main.c"wm geometry . 300x200wm iconify .
Deleting windows:destroy .x
![Page 26: Tk Widgets This material is best on several sources –Slides by Dr. Ernest J. Friedman-Hill –various Tcl/Tk books](https://reader037.vdocuments.net/reader037/viewer/2022110304/551b373c550346cf5a8b643e/html5/thumbnails/26.jpg)
Summary
Creating interfaces with Tk is easy:– Create widgets.– Arrange with geometry managers.– Bind if necessary (rarely need to, except for text and
canvas objects)– window management