Building User Interfaces with Tk

2685 days ago, 861 views
PowerPoint PPT Presentation
Structure of a Tk Application. 1. Gadget hierarchy2. One Tcl interpreter3. One processcan have > 1 application in a processWidget = window with specific look and feelWidget classes actualized by Tkframe, toplevellabel, catch, checkbutton, radiobuttonmenu, menubuttonmessage, dialogentry, content, listboxcanvasscrollbar, scale.

Presentation Transcript

Slide 1

´╗┐Layout Basic Structures Widget Creation Geometry Management Widget Commands Event Bindings Interprocess Communication Misc. Charges Examples Goal Understand Tk language structure Understand Tk worked in orders Reading Ch. 19-31, Practical Programming in Tcl and Tk Building User Interfaces with Tk/Tcl

Slide 2

1. Gadget progressive system 2. One Tcl translator 3. One process can have > 1 application in a procedure Widget = window with specific look and feel Widget classes executed by Tk outline, toplevel name, catch, checkbutton, radiobutton menu, menubutton message, discourse passage, content, listbox canvas scrollbar, scale Structure of a Tk Application

Slide 3

The Widget Hierarchy . .listbox .menu .scroll .menu.file

Slide 4

Types of Windows Top-level window Main window . .dlg .listbox .menu .scroll .menu.file .dlg.msg .dlg.yes Internal windows

Slide 5

Each gadget has a class : catch, listbox, and so forth. Tcl charge named after each class, used to make occurrences catch .a.b - content Quit - summon exit scrollbar .x - situate level Creating Widgets class name setup alternatives window name

Slide 6

Defined by class for catches : activeBackground, activeForeground, grapple, foundation, bitmap, borderWidth, summon, cursor, disableForeground, textual style, frontal area, stature, padx, pady, alleviation, state, content, textVariable, width If not determined on summon line, taken from choice database stacked from RESOURCE_MANAGER property or .Xdefault s record might be set, questioned with Tcl orders choice include *Button.relief indented If not in choice database, utilize default gave by class execution Configuration Options

Slide 7

Widgets don't control their own positions and sizes, geometry chiefs do Widgets don't show up on screen until oversaw by geometry director Geometry supervisor = calculation for orchestrating slave windows in respect to ace window Parameters from application planner Geometry of ace Requested size from slave Geometry Manager Size and area of slave Requested size for ace Geometry Management

Slide 8

Simple, however not capable Each slave put separately in respect to its lord The Placer put .x - relx 0.5 \ - y 1.0c - stay n put .x - x 0 - y 0 put .x - relx 0.5 - depend 0.5 \ - tallness 3c - grapple focus put .x - relheight 0.5 \ - relwidth 0.5 - relx 0 - depend 0.5

Slide 9

Much more intense than placer Arranges gatherings of slaves together Packs slaves around edges of ace's depression For each slave all together: 1. Pick a side of the ace 2. Cut off an edge for slave 3. Perhaps develop slave to fill outline 4. Position slave in edge The Packer

Slide 10

Packer Examples pack .a - side left pack .b - side left pack .c - side left .a .b .c .a .b .c pack .a - side top - stay w pack .b - side top - grapple w \ - pady .5c pack .c - side top - stay w .a .b .a .b .c pack .a - side top - fill x pack .b - side right - fill y pack .c - padx 0.5c - pady 1c \ - fill both .a .c .b .c .a

Slide 11

Considers connections between slaves requirement like situation line and section courses of action simple to accomplish alters game plan if slave asks for an alternate size Requests measure in the interest of ace sufficiently substantial for all slaves changes if slaves ask for various sizes grants progressive geometry administration compels proliferate in pecking order Packer Advantages

Slide 12

Tcl summon for every gadget, named after gadget's way name made with gadget Used to reconfigure, control gadget catch .a.b design - alleviation indented .a.b streak scrollbar .x set 0.2 0.4 .x get Widget charge erased consequently when gadget is crushed Principle: all state ought to be coherent, modifiable, at whatever time Widget Commands

Slide 13

Question: How to make gadgets cooperate with application, different gadgets? Reply: Tcl summons Widget activities are Tcl charges catch .a.b - order leave Widgets utilize Tcl orders to speak with each other scrollbar .x - summon ".y yview" Application utilizes gadget orders to speak with gadgets Connections catch discharge leave tap on bolt .y yview 9

Slide 14

Associate Tcl with mouse/console occasions tie .t <Control-h> {backspace .t} Can choose at least one windows single window: .t all windows in a class: Text all windows: all Specifying occasions <Double-Control-ButtonPress-1> <3> <Any-KeyPress> Bindings Window/Tag Event Sequence Script Event Type Button or Keysym Modifiers

Slide 15

% substitutions in restricting scripts arranges from occasion: %x and %y window: %W character from occasion: %A and so on. Cases tie .c <B1-Motion> {move %x %y} tie .t <Any-KeyPress> {insert %A} tie all <Help> {help %W} Only one restricting triggers from every occasion for each label/window all together window before class before toplevel before all if >1 official with same label coordinate, most particular executes Bindings (cont.)

Slide 16

Binding labels occasion not bound to window, class, or all is bound to a tag any client characterized string each window has rundown of restricting labels apply occasion to each tag all together most particular restricting that matches label/occasion is executed Default window name, class name, closest toplevel predecessor name, all bindtags window ?tagList? can read/adjust label list for window include new ties, alter arrange bindtags .b {all . Catch .b} - turn around default arrange Use to oversee complex restricting sets Bindings (cont.)

Slide 17

The determination choice get choice get FILE_NAME Issuing charges to other Tk applications send tgdb "break tkEval.c:200" winfo interps returns: wish tgdb ppres Window data winfo width .x winfo youngsters .x winfo containing $x $y Other Tk Commands

Slide 18

Keyboard center concentration .x.y Communication with window administrator wm title . "Altering main.c" wm geometry . 300x300 wm iconify . Erasing windows obliterate .x Grabs snatch .x get discharge .x Access to Other Window Facilities

Slide 19

toplevel .d message - width 3i - bd 2 \ - alleviation raised - legitimize focus - textual style \ *-helvetica-medium-r-ordinary - *-240-* \ - content "Document main.c hasn't been \ spared to plate since it was last \ changed. What should I do?" pack - side top - fill both Example 1: Dialog Box

Slide 20

outline pack - side base - fill both catch - content "Spare File" \ - summon "quit spare" pack - side left \ - grow yes - padx 20 - pady 20 catch - content "Quit Anyway" \ - charge "quit" pack - side left \ - extend yes - padx 20 - pady 20 Dialog Box Cont.

Slide 21

catch - content "Come back to Editor" \ - summon "quit return" pack - side left \ - extend yes - padx 20 - pady 20 proc quit catch { puts stdout "You squeezed the \ $button catch; bye-bye" annihilate .d } Dialog Box Cont.

Slide 22

listbox .list - yscroll ".scroll set" \ - alleviation raised - width 20 - tallness 15 pack .list - side left scrollbar .scroll - charge ".list yview" pack .scroll - side right - fill y Example 2: Browser

Slide 23

if {$argc > 0} { set dir [lindex $argv 0] } else {set dir .} foreach i [exec ls - a $dir] { .list embed end $i } Browser Cont.

Slide 24

tie .list <Double-Button-1> { peruse $dir [selection get] tie .list <Control-c> {destroy .} center .rundown proc peruse {dir file} { if {$dir != "."} { set document $dir/$file } if [file isdirectory $file] { executive peruse $file & } else { if [file isfile $file] { executive $env(EDITOR) $file & } else { puts stdout "\"$file\" isn't \ a normal record or catalog" } Browser Cont.

Slide 25

Top-level canvas with x and y scrollbars 20x10 cluster of rectangles with (i,j) file mark select and print rectangle names dish canvas Example 3: Scrolling Canvas

Slide 26

Create beat level gadget Inform window administrator of size, title, and so forth proc mkScroll { get {destroy $w} toplevel $w wm geometry $w +300+300 wm title $w "Scrollable Canvas Demonstration" wm iconname $w "Canvas" wm minsize $w 100 Example 3: Scrolling Canvas

Slide 27

Create header message and quit catch message $w.msg \ - textual style - Adobe-Times-Medium-R-Normal-*-180-* \ - viewpoint 300 - help raised - bd 2 - content {This window shows a canvas gadget that can be looked over either utilizing the scrollbars or by dragging with catch 2 in the canvas. On the off chance that you click catch 1 on one of the rectangles, its records will be imprinted on stdout.} outline $w.frame - alleviation raised - bd 2 catch $w.ok - content "OK" - summon "destroy $w" pack $w.msg - side top - fill x pack $w.ok - side base - pady 5 pack $w.frame - side top - grow yes - fill both Scrolling Canvas Cont.

Slide 28

Create canvas and scrollbars settle greatest looking over/drawing area set up connection between parchment bars and canvas set c $w.frame.c canvas $c - scrollregion {-10c - 10c 50c 20c} \ - xscroll "$w.frame.hscroll set" \ - yscroll "$w.frame.vscroll set" scrollbar $w.frame.vscroll - help depressed \ - charge "$c yview" scrollbar $w.frame.hscroll - situate horiz \ - alleviation indented - summon "$c xview" pack $w.frame.vscroll - side right - fill y pack $w.frame.hscroll - side base - fill x pack $c - grow yes - fill both Scrolling Canvas Cont.

Slide 29

Draw 20x10 cluster of rectangles and marks rectangles of foundation shading illustrated in dark content "i, j" focused in rectangle set bg [$c cget - bg] for {set i 0} {$i < 20} {incr i} { set x [expr {-10 + 3*$i}] for {set j 0; set y - 10} {$j < 10} {incr j; incr y 3} { $c make rect ${x}c ${y}c [expr $x+2]c \ [expr $y+2]c - diagram dark - fill $bg - labels rect $c make content [expr $x+1]c [expr $y+1]c \ - content "$i,$j" - grapple focus - labels content } } Scrolling Canvas Cont.

Slide 30

Bind canvas thing occasions to mouse callback strategies highlight rectangle when mouse ente