anilabel.tcl 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # anilabel.tcl --
  2. #
  3. # This demonstration script creates a toplevel window containing
  4. # several animated label widgets.
  5. if {![info exists widgetDemo]} {
  6. error "This script should be run from the \"widget\" demo."
  7. }
  8. package require Tk
  9. set w .anilabel
  10. catch {destroy $w}
  11. toplevel $w
  12. wm title $w "Animated Label Demonstration"
  13. wm iconname $w "anilabel"
  14. positionWindow $w
  15. label $w.msg -font $font -wraplength 4i -justify left -text "Four animated labels are displayed below; each of the labels on the left is animated by making the text message inside it appear to scroll, and the label on the right is animated by animating the image that it displays."
  16. pack $w.msg -side top
  17. ## See Code / Dismiss buttons
  18. set btns [addSeeDismiss $w.buttons $w]
  19. pack $btns -side bottom -fill x
  20. # Ensure that this this is an array
  21. array set animationCallbacks {}
  22. ## This callback is the core of how to do animation in Tcl/Tk; all
  23. ## animations work in basically the same way, with a procedure that
  24. ## uses the [after] command to reschedule itself at some point in the
  25. ## future. Of course, the details of how to update the state will vary
  26. ## according to what is being animated.
  27. proc RotateLabelText {w interval} {
  28. global animationCallbacks
  29. # Schedule the calling of this procedure again in the future
  30. set animationCallbacks($w) [after $interval RotateLabelText $w $interval]
  31. # We do marquee-like scrolling text by chopping characters off the
  32. # front of the text and sticking them on the end.
  33. set text [$w cget -text]
  34. set newText [string range $text 1 end][string index $text 0]
  35. $w configure -text $newText
  36. }
  37. ## A helper procedure to start the animation happening.
  38. proc animateLabelText {w text interval} {
  39. global animationCallbacks
  40. # Install the text into the widget
  41. $w configure -text $text
  42. # Schedule the start of the animation loop
  43. set animationCallbacks($w) [after $interval RotateLabelText $w $interval]
  44. # Make sure that the animation stops and is cleaned up after itself
  45. # when the animated label is destroyed. Note that at this point we
  46. # cannot manipulate the widget itself, as that has already died.
  47. bind $w <Destroy> {
  48. after cancel $animationCallbacks(%W)
  49. unset animationCallbacks(%W)
  50. }
  51. }
  52. ## Next, a similar pair of procedures to animate a GIF loaded into a
  53. ## photo image.
  54. proc SelectNextImageFrame {w interval} {
  55. global animationCallbacks
  56. set animationCallbacks($w) \
  57. [after $interval SelectNextImageFrame $w $interval]
  58. set image [$w cget -image]
  59. # The easy way to animate a GIF!
  60. set idx -1
  61. scan [$image cget -format] "GIF -index %d" idx
  62. if {[catch {
  63. # Note that we get an error if the index is out of range
  64. $image configure -format "GIF -index [incr idx]"
  65. }]} then {
  66. $image configure -format "GIF -index 0"
  67. }
  68. }
  69. proc animateLabelImage {w imageData interval} {
  70. global animationCallbacks
  71. # Create a multi-frame GIF from base-64-encoded data
  72. set image [image create photo -format GIF -data $imageData]
  73. # Install the image into the widget
  74. $w configure -image $image
  75. # Schedule the start of the animation loop
  76. set animationCallbacks($w) \
  77. [after $interval SelectNextImageFrame $w $interval]
  78. # Make sure that the animation stops and is cleaned up after itself
  79. # when the animated label is destroyed. Note that at this point we
  80. # cannot manipulate the widget itself, as that has already died.
  81. # Also note that this script is in double-quotes; this is always OK
  82. # because image names are chosen automatically to be simple words.
  83. bind $w <Destroy> "
  84. after cancel \$animationCallbacks(%W)
  85. unset animationCallbacks(%W)
  86. rename $image {}
  87. "
  88. }
  89. # Make some widgets to contain the animations
  90. labelframe $w.left -text "Scrolling Texts"
  91. labelframe $w.right -text "GIF Image"
  92. pack $w.left $w.right -side left -padx 10 -pady 10 -expand yes
  93. # This method of scrolling text looks far better with a fixed-width font
  94. label $w.left.l1 -bd 4 -relief ridge -font fixedFont
  95. label $w.left.l2 -bd 4 -relief groove -font fixedFont
  96. label $w.left.l3 -bd 4 -relief flat -font fixedFont -width 18
  97. pack $w.left.l1 $w.left.l2 $w.left.l3 -side top -expand yes -padx 10 -pady 10 -anchor w
  98. # Don't need to do very much with this label except turn off the border
  99. label $w.right.l -bd 0
  100. pack $w.right.l -side top -expand yes -padx 10 -pady 10
  101. # This is a base-64-encoded animated GIF file.
  102. set tclPoweredData {
  103. R0lGODlhKgBAAPQAAP//////zP//AP/MzP/Mmf/MAP+Zmf+ZZv+ZAMz//8zM
  104. zMyZmcyZZsxmZsxmAMwzAJnMzJmZzJmZmZlmmZlmZplmM5kzM2aZzGZmzGZm
  105. mWZmZmYzZmYzMzNmzDMzZgAzmSH+IE1hZGUgd2l0aCBHSU1QIGJ5IExARGVt
  106. YWlsbHkuY29tACH5BAVkAAEALAAAAAAqAEAAAAX+YCCOZEkyTKM2jOm66yPP
  107. dF03bx7YcuHIDkGBR7SZeIyhTID4FZ+4Es8nQyCe2EeUNJ0peY2s9mi7PhAM
  108. ngEAMGRbUpvzSxskLh1J+Hkg134OdDIDEB+GHxtYMEQMTjMGEYeGFoomezaC
  109. DZGSHFmLXTQKkh8eNQVpZ2afmDQGHaOYSoEyhhcklzVmMpuHnaZmDqiGJbg0
  110. qFqvh6UNAwB7VA+OwydEjgujkgrPNhbTI8dFvNgEYcHcHx0lB1kX2IYeA2G6
  111. NN0YfkXJ2BsAMuAzHB9cZMk3qoEbRzUACsRCUBK5JxsC3iMiKd8GN088SIyT
  112. 0RAFSROyeEg38caDiB/+JEgqxsODrZJ1BkT0oHKSmI0ceQxo94HDpg0qsuDk
  113. UmRAMgu8OgwQ+uIJgUMVeGXA+IQkzEeHGvD8cIGlDXsLiRjQ+EHroQhea7xY
  114. 8IQBSgYYDi1IS+OFBCgaDMGVS3fGi5BPJpBaENdQ0EomKGD56IHwO39EXiSC
  115. Ysgxor5+Xfgq0qByYUpiXmwuoredB2aYH4gWWda0B7SeNENpEJHC1ghi+pS4
  116. AJpIAwWvKPBi+8YEht5EriEqpFfMlhEdkBNpx0HUhwypx5T4IB1MBg/Ws2sn
  117. wV3MSQOkzI8fUd48Aw3dOZto71x85hHtHijYv18Gf/3GqCdDCXHNoICBobSo
  118. IqBqJLyCoH8JPrLgdh88CKCFD0CGmAiGYPgffwceZh6FC2ohIIklnkhehTNY
  119. 4CIHHGzgwYw01ujBBhvAqKOLLq5AAk9kuSPkkKO40NB+h1gnypJIIvkBf09a
  120. N5QIRz5p5ZJXJpmlIVhOGQA2TmIJZZhKKmmll2BqyWSXWUrZpQtpatlmk1c2
  121. KaWRHeTZEJF8SqLDn/hhsOeQgBbqAh6DGqronxeARUIIACH5BAUeAAAALAUA
  122. LgAFAAUAAAUM4CeKz/OV5YmqaRkCACH5BAUeAAEALAUALgAKAAUAAAUUICCK
  123. z/OdJVCaa7p+7aOWcDvTZwgAIfkEBR4AAQAsCwAuAAkABQAABRPgA4zP95zA
  124. eZqoWqqpyqLkZ38hACH5BAUKAAEALAcALgANAA4AAAU7ICA+jwiUJEqeKau+
  125. r+vGaTmac63v/GP9HM7GQyx+jsgkkoRUHJ3Qx0cK/VQVTKtWwbVKn9suNunc
  126. WkMAIfkEBQoAAAAsBwA3AAcABQAABRGgIHzk842j+Yjlt5KuO8JmCAAh+QQF
  127. CgAAACwLADcABwAFAAAFEeAnfN9TjqP5oOWziq05lmUIACH5BAUKAAAALA8A
  128. NwAHAAUAAAUPoPCJTymS3yiQj4qOcPmEACH5BAUKAAAALBMANwAHAAUAAAUR
  129. oCB+z/MJX2o+I2miKimiawgAIfkEBQoAAAAsFwA3AAcABQAABRGgIHzfY47j
  130. Q4qk+aHl+pZmCAAh+QQFCgAAACwbADcABwAFAAAFEaAgfs/zCV9qPiNJouo7
  131. ll8IACH5BAUKAAAALB8ANwADAAUAAAUIoCB8o0iWZggAOw==
  132. }
  133. # Finally, set up the text scrolling animation
  134. animateLabelText $w.left.l1 "* Slow Animation *" 300
  135. animateLabelText $w.left.l2 "* Fast Animation *" 80
  136. animateLabelText $w.left.l3 "This is a longer scrolling text in a widget that will not show the whole message at once. " 150
  137. animateLabelImage $w.right.l $tclPoweredData 100