46
บบบบบ บบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบ บบบบบบบ บบบบ C/C++, Java, Motif, Perl, Python บบบ Tcl บบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบ บบบบบบบบ บบบบบบบบบบบบบบบบบบบบบบบ บบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบ Apple บบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบ บบบบบบบบบบบบ บบบบบบบบบบบบบบบบบบบบบบบบบบบบ บบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบ บบบบบบบ Tcl/Tk บบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบ บบบบบบบบบบบบบบ บบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบ Tcl บบบบบบบบบบบบบบบ บบบบบบบ Tcl/Tk บบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบ บบบบบบบ Tcl – Tool Command Language (บบบบบบบ tickle) บบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบ บบบบบบบบบบบบ Perl บบบบ Unix shell บบบบบบบบบบบบบบบบบบบบบบบ บบบบบบบบ Tcl shell (tclsh) บบบบบบบบ Tk (บบบบบบบ tee- kay) บบบบ Associated Toolkit บบบบบบบบบบบบบบบบบบ บบบบบบบบบบบบบบบบบบบบบ X Windows system บบบบบบบบบบบบบบ Tk บบบบบบบบบ Windowing shell (wish) บบบบบบบ Tcl/Tk บบบบบบบบบบบ John K. Outsterhoot บบบบบบบบบบบ บบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบ บบบบบบบบบบบบบบบบบบบบบบ University of California, Berkeley บบบบบบบบบ บบบบบบบบบบบบบ Tcl บบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบ บบบบบบบบบบบบบบ บบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบบ Tcl บบบบบ บบบบบบบ บบบบบบบบบบบบบบบบบบบบบบบบ Tcl/Tk บบบบบบบบบบบบบบบบบบบบบ บบบบบบบบบ บ บบบบบบบบบบ บบบบบบบบบบบบบบ บบบบบบบ บบบบบบบบบ บบบ บบบบบบบบ

Tcl/Tk - staff.cs.psu.ac.thstaff.cs.psu.ac.th/noi/cs323-554/%BB%E2%B7/g432202…  · Web viewTcl/Tk ถูกพัฒนาโดย ... คำสั่งแต่ละคำสั่งของ

Embed Size (px)

Citation preview

บทนำ�

�รพฒน�แอพพลเคชนบนระบบปฎบตก�รลนกซนนมเครองมอใหใชไดหล�ยตว เชน C/C++, Java, Motif, Perl, Python และ Tcl ซงก�รเรมตนศกษ�ก�รเขยนโปรแกรม

ดวยภ�ษ�ซ หรอจ�ว�นนนบว�เปนเรองย�ก กอนหน�นสมยทยงมก�รใชง�นเครอง Apple รนแรก นนมก�รใชภ�ษ�เบสกในก�รเขยนโปรแกรม ภ�ษ�เบสกนบว�เปนภ�ษ�หนงทเขยนง�ย เหม�ะสำ�หรบผใชท

กำ�ลงหดเขยนโปรแกรม Tcl/Tk กมลกษณะหล�ยอย�งคล�ยภ�ษ�สก นกพฒน�ส�ม�รถนำ�โปรแกรม

ทเคยเขยนม�พฒน�เปนโปรแกรมดวย Tcl จะใชเวล�ไมน�น ดงนน Tcl/Tk จงเปนท�งเลอกหนงทจะนำ�ไปใชในก�รพฒน�โปรแกรม

Tcl – Tool Command Language ( อ�นว� tickle) เปนภ�ษ�สครปต

ทตองใชตวแปรภ�ษ�หรออนเตอรพรตเตอรในก�รทำ�ง�น คล�ยกบภ�ษ� Perl หรอ Unix shell ดงนนก�รใชง�นจงตองมก�รใช Tcl shell (tclsh) และยงม Tk (อ�นว� tee-kay) เปน Associated Toolkit สำ�หรบสร�งกร�ฟกยสเซอรอนเตอรเฟสบน X Windows system ก�รเรยกใชง�น Tk ตองอ�ศย Windowing shell (wish) เชนกน

Tcl/Tk ถกพฒน�โดย John K. Outsterhoot ในขณะทเข�เปนศ�สตร�จ�รยท�ง

วศวกรรมไฟฟ� และวทย�ก�รคอมพวเตอร University of California, Berkeley โดยตอนแรกนนตงใจให Tcl เปนคำ�สงภ�ษ�ทใชสำ�หรบสงง�นแบบอนเตอรแอคทฟ

เท�นน แตตอม�ไดมก�รพฒน�ขย�ยคว�มส�ม�รถของ Tcl ใหมม�กขน ก�รพฒน�แอพพลเคชนดวย Tcl/Tk นนส�ม�รถนำ�ไปใชง�นไดบนห ล�ย ๆ แพลตฟอรม ตงแตยนกซ ลนกซ แมคอนทอซ และ วนโดวส

Why use Tcl? ขอดต�ง ๆ ของ Tcl ส�ม�รถสรปไดดงน

1. Easy to learn

กก

Tcl/Tk

Tcl เปนภ�ษ�โปรแกรมทใชง�ย สำ�หรบนกเขยนโปรแกรมทมประสบก�รณส�ม�รถเรยนร Tcl และพฒน�โปรแกรมดวย Tcl ไดอย�งรวดเรว

2. Cross-platform supportTcl ส�ม�รถทำ�ง�นไดทงบนระบบปฎบตก�ร UNIX, Windows, Macintosh และ ระบบปฏบตก�รอน ๆ ทมใชกนแพรหล�ย นนคอ Tcl script หนง

ส�ม�รถนำ�ไปทำ�ง�นไดทงบน Unix, Windows และ Macintosh และในก�ร

แสดง GUI จะมคว�มแตกต�งกนขนอยกบแตละ platform3. Ready for enterprise

Tcl เปนภ�ษ�ทมคว�มเสถยรภ�พสง เหม�ะสำ�หรบแอปพลเคชนขน�ดใหญ และวตถประสงคอน ๆ ขององคกร

4. Flexible integration ดวยก�รใช Tcl มนจะเปนก�รง�ยทจะประส�นกบองคประกอบ (Component) และ

โปรแกรมอนทมอยไดอย�งมประสทธภ�พ ตวอย�งเชน ก�รใช Tcl เปนภ�ษ�สำ�หรบ ควบคม

อปกรณ และ Protocols ททำ�ง�นเฉพ�ะอย�ง ก�รเพม GUI หรอ สวนตดตอกบเครอข�ย

ใหกบแอปพลเคชนเดม หรอ ก�รรวมแอปพลเคชนทสร�งจ�กภ�ษ� Java กบ code โปรแกรมเดมของภ�ษ� C/C++

5. Network-aware applications ไมม Platform ใดทใหคว�มสะดวกในก�รตดตอกบเครอข�ยไดง�ยเท�กบ Tcl ก�รสร�ง

เครองใหบรก�รและเครองลกข�ยส�ม�รถทำ�ไดเพยงไม 2-3 น�ท ดวย Code เพยงไมก

บรรทด เพร�ะ Tcl ไดเตรยมวธทมประสทธภ�พสำ�หรบก�รเพมสวนตดตอกบเครอข�ยใหกบแอปพล เคชนเดม

6. It’s freeTcl เปนฟรโปรแกรมส�ม�รถห�ไดท Tcl Developer Xchange และส�ม�รถทำ�ก�รแกไขใหเหม�ะสมกบคว�มตองก�รของนกพฒน�ได

โปรแกรม Tcl/Tk

ส�ม�รถทำ�ก�ร Download โปรแกรม Tcl/Tk ไดท

http://dev.scriptics.com/software/tcltk/downloadnow84.tml

2

Tcl/Tk

รปท 1 แสดง Web site ทให Download โปรแกรม Tcl/Tk

Tcl Shell

3

Tcl/Tk

Tclsh เปน shell ของ Tcl สำ�หรบก�รอ�นคำ�สงต�ง ๆ ของ Tcl จ�ก Standard input หรอ File แลวทำ�ก�รตคว�มคำ�สงนน ๆ Tclsh จะตคว�มแฟมใหเปน Tcl script ห�กก�รเรยกใช shell ปร�ศจ�ก Argument จะเปนก�รใชง�น Tcl แบบอน

เตอรแอคทฟ เมอมก�รเรยกใช Tclsh จะมพรอมต % สำ�หรบสงง�นดวยคำ�สง Tcl และจะออก

จ�ก Tclsh เมอไดรบคำ�สง exit หรอ จบแฟมขอมล

รปท 2 แสดง Tcl shell บนระบบปฏบตก�ร Windows และ Unix ต�มลำ�ดบ

Windowing ShellWish เปนโปรแกรมหนงทถอว�เปน Windowing Shell ทประกอบดวยคำ�สงต�ง ๆ ของ Tcl, ชดเครองมอของ Tk (Tk toolkit) และ สวนโปรแกรมหลกทใชสำ�หรบก�รอ�น

คำ�สงจ�ก Standard input หรอ จ�กแฟมขอมล ก�รใช Tk จะใชในก�รสร�ง

4

Tcl/Tk

Graphic Application ทส�ม�รถทำ�ง�นไดในทกระบบปฏบตก�ร เชน Unix เรยก

โดยใช wish, Windows เรยกโดยใช wish.exe และ Macintosh เรยก

โดยใช Wish เปนตน โดยอ�จจะมหม�ยเลข Version เปนสวนหนงของชอได สำ�หรบก�รเรยก

ใช ห�กเรยก wish โดยไมม argument หรอ argument ตวแรกเปน “-“ แลว shell จะอ�นคำ�สง Tcl แบบอนเตอรแอคทฟจ�ก Standard input

ถ�เรยก wish แลวต�มดวย FileName argument จะถอว� FileName นนเปนชอของ Script File หลงจ�กนน wish จะทำ�ก�รแปล Script File นนและ

ทำ�ง�นต�มคำ�สงทเขยนใน File นน

รปท 3 แสดง Windowing Shell สำ�หรบระบบปฏบตก�ร Windows

Overview of main features of Tcl1.Basic syntax

Tcl เปนภ�ษ�แบบสครปต (Script) โดยทคำ�สงต�ง ๆ จะแยกกนดวยก�รขนบรรทด

ใหม หรอ เครองหม�ยอฒภ�ค (Semicolon) และทก ๆ คำ�สงจะมลกษณะของคำ�สง เหมอนกน เชน

expr 20 + 10

5

Tcl/Tk

คำ�สงนจะทำ�ก�รคำ�นวณผลบวกของ 20 และ 10 และทำ�ก�รสงผลลพธใหเปน 30 ซง

ลกษณะของผลลพธจะเปนลกษณะของส�ยอกขระ (String) ห�กคำ�สงนนไมมคว�มหม�ย (No Meaningful) ผลลพธทไดจะเปน String ว�ง คำ�สงแตละคำ�สงของ Tcl จะประกอบดวยคำ� (Word) 1 คำ� หรอ ม�กกว� โดยทแตละคำ�จะแยกโดยใชชอง

ว�ง (Space) จ�กตวอย�งคำ�สงนจะประกอบดวยคำ�ทง หมด 4 คำ�คอ expr, 20, + และ 10 โดยคำ�แรก คอ ชอของคำ�สง สวนคำ�อน ๆ เปน Argument

ของคำ�สงนน ๆ โดยทแตละคำ�สงจะมจำ�นวนของ Argument ทใชไมเท�กนกได สำ�หรบ

คำ�สง expr ก�รเวนวรรคของคำ�ไมถอว�มคว�มสำ�คญ ดงนนจงส�ม�รถเขยนลกษณะคำ�สงไดเปน

expr 20+10

รปท 4 แสดงผลของก�รบวกเลขสองจำ�นวน

2.VariablesTcl อนญ�ตใหทำ�ก�รเกบค�ดวยก�รใชตวแปรได และใชค�นนในคำ�สงได คำ�สง set ใชในก�รกำ�หนดค�และอ�นค�ของตวแปร

set x 32 เปนก�รกำ�หนดค�ใหกบตวแปร x set x เปนก�รอ�นค�ของตวแปร x

สำ�หรบ Tcl ไมมคว�มจำ�เปนทจะตองทำ�ก�รประก�ศตวแปรขนใชง�น เพร�ะตวแปรจะถกสร�ง

ขนม�อย�งอตโนมตในครงแรกทถกใช และตวแปรของ Tcl ไมมประเภทของ ตวแปร ทำ�ให ตวแปรส�ม�รถเกบค�ต�ง ๆ ได

สำ�หรบก�รใชตวแปรในคำ�สงส�ม�รถทำ�ได เชน

expr $x*3

6

Tcl/Tk

ก�รใชเครองหม�ย $ ในคำ�สงจะเปนก�รระบว�คำ�หรอตวเลขทต�มหลงนนเปนชอของตวแปร

และจะทำ� ก�รแทนค�ของตวแปร (Variable substitution) ตวนนดวยค�

ของมน เพร�ะฉะนนจ�กตวอย�งน Actual argument ของคำ�สง expr คอ 32*3 ( โดยทตวแปร x ไดถกกำ�หนดไวในตวอย�งกอนหน�น) ก�รทำ� Variable substitution นนยงส�ม�รถใชกบคำ�ทเปนคำ�สงไดดวย เชน

set cmd exprset x 11$cmd $x*$x

7

รปท 5 แสดงวธก�รประก�ศและก�รใชตวแปร

รปท 6 แสดงวธก�รคำ�นวณดวยก�รใชตวแปร

Tcl/Tk

รปท 7 แสดงก�รใช Variable substitution กบคำ�สง expr จ�กตวอย�งนเหนว�มก�รใชตวแปร cmd เกบคำ�สง expr ไว ดงนนคำ�สงในบรรทดท

3 จะหม�ยถง expr $x*$x

3.Command substitutionวธก�รทมก�รใชผลลพธของคำ�สงหนง ม�เปน Argument ของคำ�สงอน ๆ เรยกวธก�ร

นว� Command substitution เชน

set a 44set b [expr $a*4]

ก�รใชเครองหม�ย [ ] จะเปนก�รกำ�หนดให Tcl ทำ�ง�นทก ๆ อย�งทอยใน [ ] เปนแบบ Nested Tcl command นนคอ Tcl จะทำ�ก�รแทนค�ผลลพธทไดจ�กคำ�

สงใน [ ] เพร�ะฉะนน Argument ของคำ�สงทแสดงในก�ร set ครงทสองจะเปน 176

รปท 8 แสดงก�รใชง�น Command substitution

4.Quotes and Braces ก�รใช Double-quotates (“ ”) เปนก�รสร�งกลมของคำ�ใหเปน

Argument เดยว โดยทเครองหม�ย $ และคำ�ทอยในเครองหม�ย [ ] ยงคงถกแปล เพอใหทำ�ง�น เชน

set x 24set y 18set z “$x + $y is [expr $x + $y]”

8

Tcl/Tk

หลง จ�กทำ�ทง 3 คำ�สงแลว ตวแปร z จะมค�เปน 24 + 18 is 42 ในก�รทำ�ง�น

ทก ๆ อย�งทอยใน Double-quotes จะถกสงไปยงคำ�สง set เหมอนเปนคำ�

เดยว จ�กตวอย�งห�กไมมเครองหม�ย Double-quotes คำ�สง set จะม Argument อย 6 ตว คอ z, $x, +, $y, is, [expr $x + $y] ซงจะสงผลใหเกดคว�มผดพล�ด

หม�ยเหต

1. คำ�สง และตวแปรจะยงคงทำ�ง�นอยถงแมว�มนจะอยใน Double-quotes

2. จะไมมก�รสงเครองหม�ย “ ” เข�ไปยงคำ�สง

สำ�หรบก�รใช Curly braces ({ }) จะเปนก�รสร�งกลมคำ�เหมอนกบ Double-quotes แตต�งกนทจะไมมก�รแทนค�ต�ง ๆ ไมว�จะเปน $ หรอ [ ] เชน

set z {$x + $y is [expr $x + $y]} จ�กคำ�สงนตวแปร z จะมค�เปน “$x + $y is [expr $x + $y]”

รปท 9 แสดงตวอย�งก�รใช Quotes และ Braces

5.Control structuresTcl มคำ�สงทใชในก�รควบคมก�รทำ�ง�นต�ง ๆ คอ

- โปรแกรมยอย (Procedures)- คำ�สงเงอนไขก�รทำ�ง�น (Condition execution)- คำ�สงเกยวกบก�รวนทำ�ซำ� (Looping)

9

Tcl/Tk

คำ�สงในก�รสร�งโปรแกรมยอยจะใชคำ�สง proc โดย ทคำ�สง proc ประกอบดวย Argument 3 ตวคอ

- ชอของ Procedure- ชดของ Argument- สวน Body โปรแกรม

โครงสร�งของ Procedure มลกษณะดงน

proc Procedure_Name {Argument_Name1 Argument_Name2 …} {

Body}

ตวอย�ง แสดงก�รสร�ง Procedure ของ Tcl ชอ time สำ�หรบแสดงวน-เวล�ปจจบน

proc time {} {set seconds [clock seconds]puts “The time is [clock format

$seconds]”}

10

Tcl/Tk

รปท 10 แสดงผลก�รทำ�ง�นของ Procedure time

จ�กตวอย�งนส�ม�รถทำ�ก�รรวมคำ�สงใหเหลอเพยงคำ�สงเดยวไดดงน

proc time {} {puts “The time is [clock format

[clock seconds]]”}

ตวอย�ง แสดงโปรแกรม Factorial ในลกษณะก�รเรยกใชตวเอง (Recursive) ทมก�รใชคำ�สงก�รเลอกทำ� (if)

proc fac {x} {if {$x < 0} {

error “Invalid argument $x : must be a positive integer”

} elseif {$x <= 1} {return 1

} else {return [expr $x*[fac [expr $x –

1]]]}

}

คำ�สง return เปนคำ�สงในก�รออกจ�กโปรแกรมยอยนพรอมทงค�ทมอยในตวแปรใหเปนผลลพธของโปรแกรมยอย

ในก�รเรยกใชคำ�สง fac ตองใส Argument 1 ค� ใหกบตวแปร x เขน

fac 0fac 1fac 5fac -1

11

Tcl/Tk

รปท 11 แสดงผลก�รทำ�ง�นของ Procedure fac

ตวอย�ง แสดงก�รสร�ง Procedure และคำ�สงเกยวกบก�รวนทำ�ซำ� (while) ของ Tcl ชอ power

proc power {base p} {set result 1while {$p > 0} {

set result [expr $result * $base]set p [expr $p – 1]

}return $result

{ สำ�หรบก�รเรยกใชคำ�สง power จะตองใส Argument ให 2 ตว โดยท

Argument ตวแรกจะเปนก�รกำ�หนดค�ใหกบตวแปร base สวน Argument ตวทสองจะเปนก�รกำ�หนดค�ใหกบตวแปร p เชน

12

Tcl/Tk

power 2 6power 1.15 5

รปท 12 แสดงก�รสร�งและใชง�นคำ�สง power

คำ�สง while ประกอบดวย Argument 2 ตว คอ สวนทเปน Expression และเปนสวน Body ทถอว�เปน Tcl script ตวอน ซงจะม

ก�รทำ�ง�นในสวนนซำ�แลวซำ�อกในขณะท Express เปนจรง (True/Nonzero) จนกระทงค�ทไดจ�ก Expression เปนเทจ (False/Zero)

ตวอย�ง ก�รใชคำ�สง for สำ�หรบก�รแสดงผลตวเลขตงแต 0-9for {set x 0} {$x<10} {incr x} {

puts "x is $x"}

จ�กตวอย�งจะเหนว�คำ�สง for ประกอบดวย Argument 4 ตว คอ สวนทเปนก�ร

ประก�ศค�เรมตน สวน Expression สวนทใชสำ�หรบปรบเปลยนค�ก�รวนทำ�ซำ� และ

สวน Body ซงมโครงสร�งคล�ยกบภ�ษ� C/C++

13

Tcl/Tk

รปท 13 แสดงวธก�รใชคำ�สง for

6.Where do commands come from คำ�สงของ Tcl ส�ม�รถสร�งไดจ�ก 3 ท�งคอ

คำ�สงกลมท 1 เปนคำ�สงทตวแปลภ�ษ� (Interpreter) Tcl ไดเตรยมไวใหแลว กลมคำ�

สง เหล�นจะเรยกว� “Built-in commands” เชน

BaseHttpSaftSaft TclTclafterappendarrayauto_execokauto_importauto_load

auto_mkindexauto_resetbgerrorbinarybreakcasecatchclockcloseconcatcontinuedee

encodingeoferrorevalexecexitexprfblockedfconfigurefcopyfileflush

14

Tcl/Tk

forforeachformatgetsglobglobalhistoryifincrinfointerpjoinlistllengthloadlsortmemorymsgcat

namespaceopenpackageparraypidprocputspwdreadregexpregistryregsubretrunresourcescanseeksetsocket

sourcesplitstringsubsetswitchtcl_endOfWordtcl_findLibrarytelltimetraceunknownunsetupdatevariablevwaitwhile

คำ�สงกลมท 2 เปนกลมคำ�สงทถกสร�งจ�กก�รใชกลไกก�รขย�ย (Extension mechanism) ของ Tcl นนคอ Tcl ไดจดเตรยม APIs (Application Programming Interfaces) ทอน

ญ�ตใหนกพฒน�ส�ม�รถสร�งคำ�สงใหมไดเองดวยก�รเขยน command procedure ในภ�ษ� C/C++Tk เปน เปนสวนขย�ยทเปนทรจกตวหนง ซงมคว�มส�ม�รถม�กในก�รสร�ง GUI

สำ�หรบสวนขย�ยอนมก�รเตรยมก�รเขยนโปรแกรมเชงวตถ (Object-Oriented Programming) ก�รเข�ถงฐ�นขอมล คว�มส�ม�รถ

ท�งกร�ฟฟก และคว�มส�ม�รถอน ๆ

คำ�สงกลมท 3 เปนกลมคำ�สงทประกอบดวย procedure ทถกสร�งโดยคำ�สง proc เชน

คำ�สง power ทไดมก�รสร�งขนจ�กตวอย�งทแสดงม�แลวข�งตน สำ�หรบก�ร extension ถกใชสำ�หรบก�รทำ�ง�นในระดบตำ�เพอใหมคว�มสะดวกในก�รเขยน

15

Tcl/Tk

โปรแกรมดวยภ�ษ� C และ procedure ถกใชในก�รทำ�ง�นในระดบสงทง�ย

ทจะเขยนใน Tcl

7.Other features คำ�สงในก�รควบคม (control structure) อน ๆ เชน foreach และ

switch มประสทธภ�พในก�รจดก�ร String ไมว�จะเปน String ทมคว�มย�วไมคงทกถก

จดก�รไดง�ยเหมอนกบตวเลข

มคว�มส�ม�รถในก�รจดก�รอปกรณ I/O เชน Disk, Network sockets เปนตน Tcl ยงอำ�นวยคว�มสะดวกในก�รตดตอสอส�รผ�น Internet

Tcl มคำ�สงในก�รจดก�รกบแฟมขอมล เชน ก�รจดก�รชอแฟมขอมล ก�รอ�น ก�รเขยน ก�รทำ�สำ�เน� ก�รลบ ก�รสร�งไดเรกทอร เปนตน

ส�ม�รถเรยกใชแอปพลเคชนอนไดดวยคำ�สง exec และ ทำ�ก�รตดตอกบแอปพลเคช นอน ๆ ทกำ�ลงทำ�ง�นอยได

มคว�มส�ม�รถในก�รจดก�รเรองวนและเวล�

ก�รจดก�ร List คอ Tcl ทำ�ก�รก�รสร�งกลมของค�ไดง�ยและก�รจดก�รกบค�เหล�นนไดหล�ยวธ

มโครงสร�งขอมลแบบ Array มก�รใชเครองหม�ย Backslash (“\” ) สำ�หรบอกขระพเศษ เชน

\b เปนก�รสงใหทำ�เวนวรรค

\n เปนก�รสงใหขนบรรทดใหม

\t เปนก�รสงใหเลอนไประยะ Tab และยงใชในก�รยตคว�มหม�ยพเศษต�ง ๆ ดวย เชน \$ เพอใหแสดงสญลกษณ $

Tcl ยอมให Script หยดรอเหตก�รณทจะเกดขนอย�งแนนอน

Tk Fundamentals

สำ�หรบสวนนจะอธบ�ยวธก�รใชพนฐ�นของ Tk โดย Tk จะรวมเอ� 35 คำ�สงของ Tcl ไว

สำ�หรบก�รสร�งและจดก�ร widget ใน Graphic User Interface ซงก�รใช

ง�น Tk จะตองทำ�บนระบบ X window, Windows และ Macintosh โดยท

สครปตเดยวกนส�ม�รถทำ�ง�นไดต�ง platforms โดยไมตองทำ�ก�รเปลยนแปลง

Widget คอ หน�ต�ง (Window) ในลกษณะของกร�ฟฟกซงมลกษณะและพฤตกรรม

เฉพ�ะอย�ง คำ�ว� Widget กบ Window มกถกใชแทนกนได Widget จะประกอบไป

ดวย Buttons, Scrollbars, Menu และ Text windows Tk

16

Tcl/Tk

ยงม Canvas ทอนญ�ตทำ�ก�รสร�งสงต�ง ๆ เชน Lines, Boxes และ Bitmaps ได

Widgets ต�ง ๆ ของ Tk ถกจดก�รแบบลำ�ดบชน (Hierarchy) โดย Window hierarchy จะหม�ยถงหน�ต�งหลกทจะประกอบไดดวยหน�ต�งลก (Children windows) จำ�นวนหนง และหน�ต�งลกยงส�ม�รถประกอบไปดวยหน�ต�งอนไดอก

Widgets อยภ�ยใตก�รควบคมของตวจดก�รท�งเรข�คณต (Geometry manager) ซงเปนตวควบคมทมประสทธภ�พม�กสำ�หรบ Tk ทใชในก�รควบคมขน�ด และ

ตำ�แหนงของ widget ต�ง ๆ บนจอภ�พ วธหลกคอก�รท Geometry manager ใช frame ซงเปน widget ตวหนงในก�รบรรจ widgets อน ๆ

โดยท widgets ต�ง ๆ ทถกสร�งขนจะถกจดใหอยใน frame และก�รใช frame ภ�ยใน frame อกชนจะทำ�ใหส�ม�รถสร�งแผนผงออกแบบทมคว�มซบซอนได

Tk widget-creation commands

Command

Description

button Create and manipulate button widgets.

canvas Create and manipulate canvas widgets.

checkbutton

Create and manipulate checkbutton widgets that is linked to a Tcl variable.

entry Create and manipulate entry widgets (one-line text entry).

frame Create and manipulate frame widgets (container widget) used with geometry managers.

Command

Description

label Create and manipulate label widgets (read-only, one-line text label).

17

Tcl/Tk

listbox Create and manipulate listbox widgets (line-oriented, scrolling text widget).

menu Create and manipulate menu widgets.

menubutton

Create and manipulate menubutton widgets that posts a menu.

message Create and manipulate message widgets (read-only, multiline text message).

radiobutton

Create and manipulate radiobutton widgets that is one of a set of radio buttons linked to one variable.

scale Create and manipulate scale widgets that adjusts the value of a variable.

scrollbar Create and manipulate scrollbar widgets that can be linked to another widget.

spinbox Create and manipulate spinbox widgets.

text Create and manipulate text widgets (editable).

toplevel Create and manipulate toplevel widgets (a frame that is a new top level window).

Tk widget-manipulation commands

Command

Description

bell Ring the terminal bell device.bind Bind a Tcl command to an event.

18

Tcl/Tk

bindtages Create binding classes and control binding inheritance.

clipboard Manipulate the clipboard.destroy Delete a widget.Command

Description

event Define and generate virtual events.

focus Control the input focus.font Set and query font attributes and

measurements.grab Steal the input focus from other

widgets.grid Arrange widgets into a grid with

constraints.image Create and manipulate images.lower Lower a window in the stacking

order.option Set and query the resources

database.pack Pack a widget in the display with

constraints.place Place a widget in the display with

positions.raise Raise a window in the stacking

order.selection Manipulate the selection.send Send a Tcl command to another

Tk application.tk Query or set the application

name.tkerror Handler for background error.tkwaite Wait for an event.update Update the display by going

through the event loop.winfo Query window state.

19

Tcl/Tk

wm Interact with the window manager.

Tk support procedures

Command Descriptiontk_bisque Install bisque family of colors.tk_chooseColor

Dialog to select a color. (Tk 4.2)

tk_dialog Create simple dialogs.tk_focusFollowsMouse

Install mouse-tracking focus model.

tk_focusNext Focus on next widget in tab order.

tk_focusPrev Focus on previous widget in tab order.

Command Descriptiontk_getOpenFile

Dialog to open an existing file. (Tk 4.2)

tk_getSaveFile

Dialog to save file. (Tk 4.2)

tk_messageBox

Message dialog. (Tk 4.2)

tk_optionMenu

Create an option menu.

tk_popup Create a pop-up menu.tk_setPalette Set the standard color

palette. (Tk 4.2)

ตวอย�งก�รใช Button: Hello, World! in Tk ใหทำ�ก�รพมพคำ�สงดงนทพรอมตของ wish

%button .b -text “Hello” –command {put stdout “Hello, World!”}

20

Tcl/Tk

%pack .b –padx 20 –pady 10%.b configure –background Green –font

"CordiaUPC 30 bold italic underline" คำ�สงในบรรทดแรกเปนก�รสร�งปมขนม�หนงปมชอว� .b โดยทบนปมจะ แสดงขอคว�ม Hello

และคำ�สงทจะใหเกดขนเมอมก�รกดปมน คอ ใหแสดงคำ�ว� Hello, World! ท standard output แตปมนจะยงไมแสดงใหเหน จนกว�จะทำ�ก�ร pack ปมทสร�ง

ขนจงจะปร�กฏ และทำ�ก�รปรบขน�ดวนโดวใหพอดกบปมทนท เมอทำ�ก�รปอนคำ�สงทส�มจะเปนก�ร

เปลยนสของปมใหเปนสเขยว ซงก�รระบสอ�จใช #00ff00 (#RRGGBB) และกำ�หนดรปแบบอกษรแบบ CordiaUPC ขน�ด 30 มลกษณะเปนตวหน� เอยง

และขดเสนใต

รปท 14 แสดงหน�ต�งจ�กทไดจ�กตวอย�ง Hello, World!Tk ใช Object-base system สำ�หรบก�รสร�งและก�รตงชอใหกบ widgets แต Tk ไมไดเปน Object-oriented อย�งเตมรปแบบ ซง

คว�มสมพนธของแตละ class ของ widget ( เชน button) จะเปนคำ�สง

สำ�หรบก�รสร�ง instance ของ class นน คำ�สงใหมจะถกกำ�หนดใหทำ�ง�นกบ

instance ของ widget นน จ�กตวอย�งทไดแสดงข�งตนจะส�ม�รถกระทำ�ก�ร

กบปม .b ดวยก�รอ�ศยชอของปมนนได เชน

- ก�รทำ�ใหเกด Highlight ในชวงพรบต�

%.b flash- ก�รเรยกใชปม

%.b invokeHello, World!

ห�กตองก�รเปลยนแปลงก�รทำ�ง�นเมอเกดเหตก�รณก�รกดปม โดยใหแสดงเปนกรอบขอคว�ม

แสดง Hello, World! ทำ�ไดโดยปอนคำ�สงตอไปน

21

Tcl/Tk

% - -.b configure command {tk_messageBox m essage "Hello, World!" \

>-type ok -title "Hello"} จะเหนว�ในคำ�สงจะมเครองหม�ย “\” เพอเปนก�รตอคำ�สงทย�วกว�ทจะจบไดในบรรทดเดยว

ผลทไดจ�กก�รกดปมจะเปนดงรป

รปท 15 แสดงเหตก�รณเมอทำ�ก�รกดปม Hello หลงทำ�ก�รเปลยนแปลงคำ�สง

Widget มกระบวนก�รทำ�ง�น cget สำ�หรบก�รสอบถ�มคณลกษณะปจจบนของมน เชนตองก�รสอบถ�มว�ปมทไดสร�งขนม�แลวนนมสอะไร

%.b cget –background Green

และยงส�ม�รถขอร�ยละเอยดของ Widget ไดม�กขนดวยก�รใชคำ�สง configure ท ไมมก�รกำ�หนดค�ให ดงน

%.b configure –background -background background Background

SystemButtonFace Green ส�รสนเทศทไดประกอบดวย คำ�สงทเปน Command-line switch,ชอทรพย�กร

(Resource name), ชอคล�ส (Class name), ค�เรมตนของวตถ (Default value) และ ค�ของวตถในปจจบน (Current value) ต�มลำ�ดบ

22

Tcl/Tk

ก�รใชง�น Tcl แบบอนเตอรแอคทฟ

จ�กตวอย�งต�ง ๆ ทไดเสนอในตอนตนเปนก�รใชง�น Tcl แบบอนเตอรแอคทฟทงหมด นนคอเปนก�ร

ใชง�นคำ�สงต�ง ๆ ของ Tcl ทนทท พรอมต ของ Tclsh ก�รใชง�น Tcl แบบอนเตอรแอคทฟจะคล�ยกบททำ�ไดบนภ�ษ�เบสกรนเก�ทเปน อนเตอร

พรตเตอร ก�รใชง�นแบบนจะไมคอยมประโยชนเท�ไร ก�รใชง�น Tcl นอกจ�กวธนแลว ยงมวธใชง�น Tcl แบบนอนอนเตอรแอคทฟอกวธหนงดวย

ก�รใชง�น Tk แบบอนเตอรแอคทฟ

จ�กทไดกล�วไวตอนตนว�ก�รใช Tk จะตองใชเซลลวนโดวสกอน สำ�หรบก�รใชง�นในลกษณะนจะมวธ

เดยวกบก�รใชง�น Tcl แบบอนเตอรแอคทฟ นนคอก�รพมพคำ�สงทพรอมตเพอสงง�น ใน Wish

ก�รใชง�น Tcl/Tk แบบนอนอเตอรแอคทฟ

ก�รใช Tcl/Tk แบบนอนอนเตอรแอคทฟคอ ก�รเขยนสครปตคำ�สงเกบไวในไฟลกอน จ�กนนจงเรยก tclsh หรอ wish ม�แปลคำ�สงในสครปตทไดเขยนอกครงหนง วธนมประโยชนม�กกว�ก�รใชง�น

แบบอนเตอรแอคทฟ เนองจ�กทำ�ใหส�ม�รถสงง�นไดตอเนองดวยชดคำ�สง ทำ�ใหได แอปพลเคชนทเตมรป

แบบ สมมตว�ไดทำ�ก�รเขยนสครปตไวในไฟล myprog.tcl แลวจะเรยกใชโปรแกรมทเขยนม�ทำ�ง�นทยนกสพรอมตไดดงน

$tclsh myprog.tclหรอ

$wish myprog.tcl

นอกจ�กนยงส�ม�รถเรยกชอสครปตไดโดยตรง แตในไฟลสครปตจะตองมคำ�สง Shell ดงน

#!/bin/shexec wish “$0” “#@”

วธก�รนชวยปองกนคว�มผดพล�ดทเกดจ�กก�รตดตง Tcl/Tk ไวในไดเรคทอรทต�งกน หลงจ�กคำ� สงเชลลทงสองบรรทดนแลวจงจะเปนคำ�สงอน ๆ ของโปรแกรมทเขยนขน นอกจ�กนถ�ตองก�รเรยกชอ

ไฟลสครปตจ�กยนกซพรอมตใหขนม�ทำ�ง�นไดเองโดยไมตองเรยก tclsh หรอ wish กอนกตองใชคำ�สง

chmod u+x myprog.tcl

23

Tcl/Tk

เพอเปนก�รเปลยนโหมดไฟลใหเปน execute จงจะทำ�ง�นได

ห�กนำ�โปรแกรม power ทเคยทำ�แบบอนเตอรแอคทฟ ม�เขยนแบบ Tcl Scrip File ส�ม�รถเขยนไดดงน

File name: power.tcl#!/usr/local/bin/tcl8.0/unix/tclsh

set base [lindex $argv 0]set p [lindex $argv 1]set result 1

puts "Program : $argv0"puts "Base : $base"puts "Power : $p"

while {$p > 0} { set result [expr $result*$base] set p [expr $p-1]}puts stdout "Result is $result"

สำ�หรบบรรทดแรกเปนก�รระบตำ�แหนงของ tclsh เพอใหไฟล Script นส�ม�รถทำ�ง�นไดทนท ซงจะตองทำ�ก�รเปลยน mode ของไฟลในส�ม�รถ execute ไดกอน

24

Tcl/Tk

รปท 16 แสดงผลก�รใชง�น power แบบนอนอนเตอรแอคทฟทมก�รเรยกใช tclsh

รปท 17 แสดงผลก�รใชง�น power แบบนอนอนเตอรแอคทฟทไมมก�รเรยกใช tclsh

ตอไปจะเปนตวอย�งก�รเขยน Tk แบบนอนอนเตอรแอคทฟ สำ�หรบก�รคำ�นวณเลขยกกำ�ลง ซง

ทำ�ก�รเขยน Script file ไดดงน

File name: powerW.tcl# This script for Wish

# compute power base^pproc power {base p} {

set result 1while {$p > 0} {

set result [expr $result * $base]set p [expr $p - 1]

}return $result

25

Tcl/Tk

}

# set main window attributeset mainWindow .maintoplevel $mainWindowwm title .main "Power Calculator for wish"$mainWindow config -background #00ffff# create a labellabel $mainWindow.msg -font "CordiaUPC 20 bold" \

-text "This script is used to compute the power"pack $mainWindow.msg$mainWindow.msg config -background pink2

# create two labels to print base & powerframe $mainWindow.frameLeftpack $mainWindow.frameLeft -side left -padx 10 -pady 5

label $mainWindow.frameLeft.base -text "Base : "label $mainWindow.frameLeft.power -text "Power : "pack $mainWindow.frameLeft.base $mainWindow.frameLeft.power -side top

# create two textboxes for receive input base & powerframe $mainWindow.frameCenterpack $mainWindow.frameCenter -side left -padx 0 -pady 5

entry $mainWindow.frameCenter.base -width 20 -textvariable baseStringentry $mainWindow.frameCenter.power -width 20 -textvariable powerString

26

Tcl/Tk

pack $mainWindow.frameCenter.base $mainWindow.frameCenter.power -side top

# create buttonframe $mainWindow.frameRightpack $mainWindow.frameRight -side bottom -padx 10 -pady 5button $mainWindow.frameRight.butCompute -text "Compute Now" \

-command {tk_messageBox -message "Result of \

$baseString ^ $powerString = [power $baseString $powerString]" \

-type ok -title "Power"}pack $mainWindow.frameRight.butCompute

รปท 18 แสดงก�รใชง�นโปรแกรม Power ในลกษณะทม GUI

27

Tcl/Tk

ภ�คผนวก ก.

Variables define by tclsh and wish

Variable DescriptionArgc The number of command-line

arguments.Argv A list of the command-line

arguments.argv0 The name of the script being

executed. If being used interactively, argv0 is the name of the shell program.

Embed_args The list of arguments in the <EMBED> tag. Tcl applets only.

Env An array of the environment variables.

Tcl_interactive

True (one) if the tclsh is prompting for commands.

Tcl_library The script library directory.Tcl_patchLe Modified version number, e.g.,

28

Tcl/Tk

vel 8.0b1.Tcl_platform Array containing operation

system information.Tcl_pkgPath List of directories to search for

packages.Tcl_prompt1 If defined, this is a command

that outputs the prompt.Tcl_prompt2 If defined, this is a command

that outputs the prompt if the current command is not yet complete.

Tcl_version Version number.Auto_path The search path for script

library directories.Auto_index A map from command name to

a Tcl command that defines it.Auto_noload If set, the library facility is

disabled.Auto_noexec

If set, the auto execute facility is disabled.

Geometry (wish only). The value of the –geometry argument.

ภ�คผนวก ข.Core Tcl Commands

Command

Description

after Schedule a Tcl command for later execution.

append Append arguments to a variable’s value. No spaces added.

array Query array state and search

29

Tcl/Tk

through elements.binary Convert between strings and

binary data.break Premature loop exit.catch Trap errors.cd Change working directory.clock Get the time and format data

strings.close Close an open I\O stream.concat Concatenate arguments with

spaces between. Splices lists.console Control the console used to enter

commands interactively.continue Continue with next loop iteration.error Raise an error.eof Check for end of file.eval Concatenate arguments and

evaluate them as a command.exec Fork and execute a UNIX program.expr Evaluate a math expression.fblocked Poll an I/O channel to see if data is

ready.fconfigure

Set and query I/O channel properties.

fcopy Copy from one I/O channel to another.

file Query the file system.fileevent Register callback for event-driven

I/O.flush Flush output from an I/O stream’s

internal buffers.for Loop construct similar to C for

statement.Command

Description

foreach Loop construct over a list, or lists,

30

Tcl/Tk

of values.format Format a string similar to c sprintf.gets Read a line of input from an I/O

stream.glob Expand a pattern to matching file

names.global Declare global variables.history Command-line history control.if Conditional command. Allows else

and elseif clause.incr Increment a variable by an integer

amount.info Query the state of the Tcl

intepreter.interp Create additional Tcl interpreters.join Concatenate list elements with a

given separator string.lappend Add elements to the end of a list.lindex Fetch an element of a list.linsert Insert elements of a list.list Create a list out of the arguments.llength Return the number of elements in

a list.load Load shared libraries that define

Tcl commands.lrange Return a range of list elements.ireplace Replace elements of a list.lsearch Search for an element of a list that

matches a pattern.lsort Sort a list.namespace

Create and manipulate namespaces.

open Open a file or process pipeline for I/O.

package Provide or require code packages.pid Return the process ID.

31

Tcl/Tk

proc Define a Tcl procedure.puts Output a string to an I/O stream.pwd Return the current working

directory.read Read blocks of character from an

I/O stream.Command

Description

regexp Regular expression matching.regsub Substitutions based on regular

expressions.rename Change the name of a Tcl

command.return Return a value from a procedure.scan Parses a string according to a

format specification.seek Set the seek offset of an I/O

stream.set Assign a value to a variable.socket Open a TCP/IP network

connection.source Evaluate the Tcl commands in a

file.split Chop a string up into list

elements.string Operate on strings.subset Substitutions with out command

evaluation.switch Multi-way branch.tell Return the current seek offset of

an I/O stream.time Measure the execution time of a

command.trace Monitor variable assignments.unknown Unknown command handler.unset Delete variable.

32

Tcl/Tk

uplevel Execute a command in different scope.

upvar Reference a variable in a different scope.

variable Declare namespace variables.vwait Wait for a variable to be modified.while A loop construct.

ภ�คผนวก ค.

Built-in Math Function

abs(arg)Returns the absolute value of arg. Arg may be either integer or floating-point, and the result is returned in the same form.

acos(arg)Returns the arc cosine of arg, in the range [0,pi] radians. Arg should be in the range [-1,1].

asin(arg)Returns the arc sine of arg, in the range [-pi/2,pi/2] radians. Arg should be in the range [-1,1].

atan(arg)Returns the arc tangent of arg, in the range [-

pi/2,pi/2] radians.

33

Tcl/Tk

atan2(x, y)Returns the arc tangent of y/x, in the range [-pi,pi] radians. x and y cannot both be 0.

ceil(arg)Returns the smallest integer value not less

than arg.

cos(arg)Returns the cosine of arg, measured in

radians.

cosh(arg)Returns the hyperbolic cosine of arg. If the result would cause an overflow, an error is returned.

double(arg)If arg is a floating value, returns arg, otherwise converts arg to floating and returns the converted value.

exp(arg)Returns the exponential of arg, defined as earg. If the result would cause an overflow, an error is returned.

floor(arg)Returns the largest integral value not greater

than arg.

fmod(x, y)Returns the floating-point remainder of the division of x by y. If y is 0, an error is returned.

34

Tcl/Tk

hypot(x, y)Computes the length of the hypotenuse of a

right-angled triangle (x*x+y*y).

int(arg)If arg is an integer value, returns arg, otherwise converts arg to integer by truncation and returns the converted value.

log(arg)Returns the natural logarithm of arg (ln arg).

Arg must be a positive value.

log10(arg)Returns the base 10 logarithm of arg

(log10arg). Arg must be a positive value.

pow(x, y)Computes the value of x raised to the power y. If x is negative, y must be an integer value.

rand()Returns a floating point number from zero to just less than one or, in mathematical terms, the range [0,1). The seed comes from the internal clock of the machine or may be set manual with the srand function.

round(arg)If arg is an integer value, returns arg, otherwise converts arg to integer by rounding and returns the converted value.

sin(arg)Returns the sine of arg, measured in radians.

35

Tcl/Tk

sinh(arg)Returns the hyperbolic sine of arg. If the result would cause an overflow, an error is returned.

sqrt(arg)Returns the square root of arg. Arg must be

non-negative.

srand(arg)The arg, which must be an integer, is used to reset the seed for the random number generator. Returns the first random number from that seed. Each interpreter has it's own seed.

tan(arg)Returns the tangent of arg, measured in

radians.

tanh(arg)Returns the hyperbolic tangent of arg.

36

Tcl/Tk

บรรณ�นกรม

ว�รส�ร

ขจร โรจนเมธนทร. Tcl/Tk Part1 ไมโครคอมพวเตอร ปท 18 ฉบบท 185 ธนว�คม 2543.

หน� 215-217.

เวปไซต

Avia Training and Consulting, LLC. http://www.avia-training.com/tcl-overview.html. [online].

28-1-44.

Eric Foster-Johnson. Graphical Applications with Tcl and Tk.

http://www.pconline.com/~erc/tclbook.htm. [online]. 29-1-44

Brent Welch. The 2nd Edition of Practical Programming in Tcl and Tk

37

Tcl/Tk

http://www.beedub.com/book/2nd/booktoc.mif.html. [online]. 28-1-44.

Tcl Developer Xchange. An Introduction to Tcl Syntax.

http://www.scriptics/scripting/syntax.html. [online]. 28-1-44.

Tcl Developer Xchange. Tcl Primer. http://www.scriptics.com/advocacy/primer.html.[online].

28-1-44.

38