summaryrefslogtreecommitdiffstats
path: root/library/demos
diff options
context:
space:
mode:
authorcsaba <csaba>2023-06-22 12:53:53 (GMT)
committercsaba <csaba>2023-06-22 12:53:53 (GMT)
commit1c0e7e8d9fc0af72fe31e347374aa13e9a0fc055 (patch)
treebc312895b9d39cd4a007bbcaeec673f097586b98 /library/demos
parenta2a53e76d1e9108d66095f9a8ba7cba7939deb56 (diff)
downloadtk-1c0e7e8d9fc0af72fe31e347374aa13e9a0fc055.zip
tk-1c0e7e8d9fc0af72fe31e347374aa13e9a0fc055.tar.gz
tk-1c0e7e8d9fc0af72fe31e347374aa13e9a0fc055.tar.bz2
Widget Demo: Code simplification and improvements.
Diffstat (limited to 'library/demos')
-rw-r--r--library/demos/anilabel.tcl2
-rw-r--r--library/demos/goldberg.tcl32
-rw-r--r--library/demos/image1.tcl2
-rw-r--r--library/demos/items.tcl2
-rw-r--r--library/demos/knightstour.tcl27
-rw-r--r--library/demos/label.tcl3
-rw-r--r--library/demos/print.tcl2
-rw-r--r--library/demos/twind.tcl2
-rw-r--r--library/demos/windowicons.tcl2
9 files changed, 40 insertions, 34 deletions
diff --git a/library/demos/anilabel.tcl b/library/demos/anilabel.tcl
index 5e08ba4..be1b402 100644
--- a/library/demos/anilabel.tcl
+++ b/library/demos/anilabel.tcl
@@ -92,7 +92,7 @@ proc animateLabelImage {w imageData interval} {
# display's DPI scaling level. Since the zooom factor must be an integer,
# the copy will only be effectively magnified if $tk::scalingPct >= 200.
set image2 [image create photo]
- set zoomFactor [expr {int($tk::scalingPct / 100.0)}]
+ set zoomFactor [expr {$tk::scalingPct / 100}]
$image2 copy $image -zoom $zoomFactor
# Install the image copy into the widget
diff --git a/library/demos/goldberg.tcl b/library/demos/goldberg.tcl
index c013ab1..483f4df 100644
--- a/library/demos/goldberg.tcl
+++ b/library/demos/goldberg.tcl
@@ -70,7 +70,7 @@ place $w.hide -in $w.msg -relx 1 -rely 0 -anchor ne
array set animationCallbacks {}
bind $w <Destroy> {
if {"%W" eq [winfo toplevel %W]} {
- unset S C speed
+ unset S C delays
}
}
@@ -78,7 +78,8 @@ set S(title) "Tk Goldberg"
set S(speed) 5
set S(cnt) 0
set S(message) "\\nWelcome\\nto\\nTcl/Tk"
-array set speed {1 10 2 20 3 50 4 80 5 100 6 150 7 200 8 300 9 400 10 500}
+array set delays \
+ {1 500 2 400 3 300 4 200 5 150 6 100 7 80 8 50 9 20 10 10}
set MSTART 0; set MGO 1; set MPAUSE 2; set MSSTEP 3; set MBSTEP 4; set MDONE 5
set S(mode) $::MSTART
@@ -176,17 +177,20 @@ proc DoCtrlFrame {w} {
bind $w.reset <Button-3> {set S(mode) -1} ;# Debugging
## See Code / Dismiss buttons hack!
- set btns [addSeeDismiss $w.ctrl.buttons $w]
- grid [ttk::separator $w.ctrl.sep] -sticky ew -pady 3p
- set i 0
- foreach b [winfo children $btns] {
+ grid [ttk::separator $w.ctrl.sep] -sticky ew -pady {3p 1.5p}
+ set btns {}
+ foreach b [winfo children [addSeeDismiss $w.ctrl.buttons $w]] {
if {[winfo class $b] eq "TButton"} {
- grid [set b2 [ttk::button $w.ctrl.b[incr i]]] -sticky ew
- foreach b3 [$b configure] {
- set b3 [lindex $b3 0]
- # Some options are read-only; ignore those errors
- catch {$b2 configure $b3 [$b cget $b3]}
- }
+ set btns [linsert $btns 0 $b] ;# Prepend
+ }
+ }
+ set i 0
+ foreach b $btns {
+ grid [set b2 [ttk::button $w.ctrl.b[incr i]]] -sticky ew -pady {1.5p 0}
+ foreach b3 [$b configure] {
+ set b3 [lindex $b3 0]
+ # Some options are read-only; ignore those errors
+ catch {$b2 configure $b3 [$b cget $b3]}
}
}
destroy $btns
@@ -298,7 +302,7 @@ proc DoButton {w what} {
}
proc Go {w {who {}}} {
- global S speed animationCallbacks MGO MPAUSE MSSTEP MBSTEP
+ global S delays animationCallbacks MGO MPAUSE MSSTEP MBSTEP
set now [clock clicks -milliseconds]
catch {after cancel $animationCallbacks(goldberg)}
@@ -319,7 +323,7 @@ proc Go {w {who {}}} {
}
set elapsed [expr {[clock click -milliseconds] - $now}]
- set delay [expr {$speed($S(speed)) - $elapsed}]
+ set delay [expr {$delays($S(speed)) - $elapsed}]
if {$delay <= 0} {
set delay 1
}
diff --git a/library/demos/image1.tcl b/library/demos/image1.tcl
index 3aa236c..c174a92 100644
--- a/library/demos/image1.tcl
+++ b/library/demos/image1.tcl
@@ -32,7 +32,7 @@ image create photo image1b \
# Create copies of the images just created, magnified according to the
# display's DPI scaling level. Since the zooom factor must be an integer,
# the copies will only be effectively magnified if $tk::scalingPct >= 200.
-set zoomFactor [expr {int($tk::scalingPct / 100.0)}]
+set zoomFactor [expr {$tk::scalingPct / 100}]
image create photo image1a2
image1a2 copy image1a -zoom $zoomFactor
image create photo image1b2
diff --git a/library/demos/items.tcl b/library/demos/items.tcl
index ad1ff4c..5f51a90 100644
--- a/library/demos/items.tcl
+++ b/library/demos/items.tcl
@@ -148,7 +148,7 @@ image create photo items.ousterhout \
-file [file join $tk_demoDirectory images ouster.png]
image create photo items.ousterhout.active -format "png -alpha 0.5" \
-file [file join $tk_demoDirectory images ouster.png]
-set zoomFactor [expr {int($tk::scalingPct / 100.0)}]
+set zoomFactor [expr {$tk::scalingPct / 100}]
foreach img {items.ousterhout items.ousterhout.active} {
image create photo ${img}2
${img}2 copy $img -zoom $zoomFactor
diff --git a/library/demos/knightstour.tcl b/library/demos/knightstour.tcl
index bc569e7..8367477 100644
--- a/library/demos/knightstour.tcl
+++ b/library/demos/knightstour.tcl
@@ -89,7 +89,8 @@ proc MovePiece {dlg last square} {
variable visited
variable delay
variable continuous
- $dlg.f.txt insert end "[llength $visited]. [N $last] .. [N $square]\n" {}
+ set line [format "%2d. %s .. %s" [llength $visited] [N $last] [N $square]]
+ $dlg.f.txt insert end $line\n
$dlg.f.txt see end
$dlg.f.c itemconfigure [expr {1+$last}] -state normal -outline black
$dlg.f.c itemconfigure [expr {1+$square}] -state normal -outline red
@@ -105,14 +106,14 @@ proc MovePiece {dlg last square} {
if {$initial == $square} {
$dlg.f.txt insert end "Closed tour!"
} else {
- $dlg.f.txt insert end "Success\n" {}
+ $dlg.f.txt insert end "Success"
if {$continuous} {
after [expr {$delay * 2}] [namespace code \
[list Tour $dlg [expr {int(rand() * 64)}]]]
}
}
} else {
- $dlg.f.txt insert end "FAILED!\n" {}
+ $dlg.f.txt insert end "FAILED!"
}
}
}
@@ -144,7 +145,8 @@ proc Exit {dlg} {
}
proc SetDelay {new} {
- variable delay [expr {int($new)}]
+ variable speed [expr {int($new)}]
+ variable delay [expr {2000 - $speed}]
}
proc DragStart {w x y} {
@@ -174,18 +176,19 @@ proc CreateGUI {} {
wm withdraw $dlg
set f [ttk::frame $dlg.f]
set c [canvas $f.c -width 192p -height 192p]
- text $f.txt -width 10 -height 1 \
- -yscrollcommand [list $f.vs set] -font {Helvetica 8}
+ text $f.txt -width 12 -height 1 -padx 3p \
+ -yscrollcommand [list $f.vs set] -font TkFixedFont
ttk::scrollbar $f.vs -command [list $f.txt yview]
- variable delay 600
+ variable speed 1400
+ variable delay [expr {2000 - $speed}]
variable continuous 0
ttk::frame $dlg.tf
- ttk::label $dlg.tf.ls -text Speed
- ttk::scale $dlg.tf.sc -from 8 -to 2000 -command [list SetDelay] \
- -variable [namespace which -variable delay]
ttk::checkbutton $dlg.tf.cc -text Repeat \
-variable [namespace which -variable continuous]
+ ttk::scale $dlg.tf.sc -from 0 -to 1992 -command [list SetDelay] \
+ -variable [namespace which -variable speed]
+ ttk::label $dlg.tf.ls -text Speed
ttk::button $dlg.tf.b1 -text Start -command [list Tour $dlg]
ttk::button $dlg.tf.b2 -text Exit -command [list Exit $dlg]
set square 0
@@ -230,14 +233,14 @@ proc CreateGUI {} {
grid columnconfigure $f 1 -weight 1
grid $f - - - - - -sticky news
- set things [list $dlg.tf.ls $dlg.tf.sc $dlg.tf.cc $dlg.tf.b1]
+ set things [list $dlg.tf.cc $dlg.tf.sc $dlg.tf.ls $dlg.tf.b1]
if {![info exists ::widgetDemo]} {
lappend things $dlg.tf.b2
if {[tk windowingsystem] ne "aqua"} {
set things [linsert $things 0 [ttk::sizegrip $dlg.tf.sg]]
}
}
- pack {*}$things -side right
+ pack {*}$things -side right -padx 3p
if {[tk windowingsystem] eq "aqua"} {
pack configure {*}$things -padx {4 4} -pady {12 12}
pack configure [lindex $things 0] -padx {4 24}
diff --git a/library/demos/label.tcl b/library/demos/label.tcl
index d92a8fa..d2823e1 100644
--- a/library/demos/label.tcl
+++ b/library/demos/label.tcl
@@ -40,8 +40,7 @@ image create photo label.ousterhout \
# display's DPI scaling level. Since the zooom factor must be an integer,
# the copy will only be effectively magnified if $tk::scalingPct >= 200.
image create photo label.ousterhout2
-label.ousterhout2 copy label.ousterhout \
- -zoom [expr {int($tk::scalingPct / 100.0)}]
+label.ousterhout2 copy label.ousterhout -zoom [expr {$tk::scalingPct / 100}]
label $w.right.picture -borderwidth 2 -relief sunken -image label.ousterhout2
label $w.right.caption -text "Tcl/Tk Creator"
diff --git a/library/demos/print.tcl b/library/demos/print.tcl
index 21315b4..a1ef1f5 100644
--- a/library/demos/print.tcl
+++ b/library/demos/print.tcl
@@ -51,7 +51,7 @@ eFy1MFd3bcQHJEQdlddkP5E1Cf9yXfbaV2d9RBAAOw==
# display's DPI scaling level. Since the zooom factor must be an integer,
# the copy will only be effectively magnified if $tk::scalingPct >= 200.
image create photo logo2
-logo2 copy logo -zoom [expr {int($tk::scalingPct / 100.0)}]
+logo2 copy logo -zoom [expr {$tk::scalingPct / 100}]
set c [canvas $w.m.c -bg white]
pack $c -fill both -expand yes -fill both -side left
diff --git a/library/demos/twind.tcl b/library/demos/twind.tcl
index cb76afe..5268fdf 100644
--- a/library/demos/twind.tcl
+++ b/library/demos/twind.tcl
@@ -181,7 +181,7 @@ image create photo img -file [file join $tk_demoDirectory images ouster.png]
# display's DPI scaling level. Since the zooom factor must be an integer,
# the copy will only be effectively magnified if $tk::scalingPct >= 200.
image create photo img2
-img2 copy img -zoom [expr {int($tk::scalingPct / 100.0)}]
+img2 copy img -zoom [expr {$tk::scalingPct / 100}]
$t image create end -image img2
diff --git a/library/demos/windowicons.tcl b/library/demos/windowicons.tcl
index 00dab35..0c1e0c0 100644
--- a/library/demos/windowicons.tcl
+++ b/library/demos/windowicons.tcl
@@ -96,7 +96,7 @@ set ::tk::icons::base_icon(.) icon
# display's DPI scaling level. Since the zooom factor must be an integer,
# the copy will only be effectively magnified if $tk::scalingPct >= 200.
image create photo icon2
-icon2 copy icon -zoom [expr {int($tk::scalingPct / 100.0)}]
+icon2 copy icon -zoom [expr {$tk::scalingPct / 100}]
pack [button $w.i -text "Set Window Icon to Globe" -image icon2 \
-compound top -command {wm iconphoto . icon}] -fill x -padx 3p