clrpick.tcl 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # clrpick.tcl --
  2. #
  3. # This demonstration script prompts the user to select a color.
  4. if {![info exists widgetDemo]} {
  5. error "This script should be run from the \"widget\" demo."
  6. }
  7. package require Tk
  8. set w .clrpick
  9. catch {destroy $w}
  10. toplevel $w
  11. wm title $w "Color Selection Dialog"
  12. wm iconname $w "colors"
  13. positionWindow $w
  14. label $w.msg -font $font -wraplength 4i -justify left -text "Press the buttons below to choose the foreground and background colors for the widgets in this window."
  15. pack $w.msg -side top
  16. ## See Code / Dismiss buttons
  17. set btns [addSeeDismiss $w.buttons $w]
  18. pack $btns -side bottom -fill x
  19. button $w.back -text "Set background color ..." \
  20. -command \
  21. "setColor $w $w.back background {-background -highlightbackground}"
  22. button $w.fore -text "Set foreground color ..." \
  23. -command \
  24. "setColor $w $w.back foreground -foreground"
  25. pack $w.back $w.fore -side top -anchor c -pady 2m
  26. proc setColor {w button name options} {
  27. grab $w
  28. set initialColor [$button cget -$name]
  29. set color [tk_chooseColor -title "Choose a $name color" -parent $w \
  30. -initialcolor $initialColor]
  31. if {[string compare $color ""]} {
  32. setColor_helper $w $options $color
  33. }
  34. grab release $w
  35. }
  36. proc setColor_helper {w options color} {
  37. foreach option $options {
  38. catch {
  39. $w config $option $color
  40. }
  41. }
  42. foreach child [winfo children $w] {
  43. setColor_helper $child $options $color
  44. }
  45. }