1#!/usr/bin/env ruby 2require 'tk' 3 4TkLabel.new(:text=><<EOT, :justify=>:left).pack 5This is a sample of bindtags and usage of 6Tk.callback_break/Tk.callback_continue. 7Please check the work of following buttons 8(attend the difference between before/after 9 pressing the bottom button), and see the 10source code. 11EOT 12 13def set_class_bind 14 TkButton.bind('ButtonPress-1', 15 proc{puts 'bind "ButtonPress-1" of TkButton class'}) 16 TkButton.bind('ButtonRelease-1', 17 proc{puts 'bind "ButtonRelease-1" of TkButton class'}) 18end 19 20# set root binding 21r = TkRoot.new 22r.bind('ButtonPress-1', proc{puts 'bind "ButtonPress-1" of root widget'}) 23r.bind('ButtonRelease-1', proc{puts 'bind "ButtonRelease-1" of root widget'}) 24 25# set 'all' binding 26TkBindTag::ALL.bind('ButtonPress-1', 27 proc{puts 'bind "ButtonPress-1" of the tag "all"'}) 28TkBindTag::ALL.bind('ButtonRelease-1', 29 proc{puts 'bind "ButtonRelease-1" of the tag "all"'}) 30 31# create buttons 32b1 = TkButton.new(:text=>'button-1', 33 :command=>proc{puts "command of button-1"}).pack 34b2 = TkButton.new(:text=>'button-2', 35 :command=>proc{puts "command of button-2"}).pack 36b3 = TkButton.new(:text=>'button-3', 37 :command=>proc{puts "command of button-3"}).pack 38b4 = TkButton.new(:text=>'button-4', 39 :command=>proc{puts "command of button-4"}).pack 40b5 = TkButton.new(:text=>'button-5', 41 :command=>proc{puts "command of button-5"}).pack 42 43# set button binding 44b1.bind('ButtonPress-1', proc{puts 'bind "ButtonPress-1" of button-1'}) 45b1.bind('ButtonRelease-1', proc{puts 'bind "ButtonRelease-1" of button-1'}) 46 47b2.bind('ButtonPress-1', proc{puts 'bind "ButtonPress-1" of button-2'}) 48b2.bind('ButtonRelease-1', proc{puts 'bind "ButtonRelease-1" of button-2'}) 49 50b3.bind('ButtonPress-1', proc{puts 'bind "ButtonPress-1" of button-3'}) 51b3.bind('ButtonRelease-1', proc{puts 'bind "ButtonRelease-1" of button-3'}) 52 53b4.bind('ButtonPress-1', proc{puts 'bind "ButtonPress-1" of button-4'}) 54b4.bind('ButtonRelease-1', proc{puts 'bind "ButtonRelease-1" of button-4'}) 55 56b5.bind('ButtonPress-1', proc{puts 'bind "ButtonPress-1" of button-5'}) 57b5.bind('ButtonRelease-1', proc{puts 'bind "ButtonRelease-1" of button-5'}) 58 59# create bindtag and set binding 60tag1 = TkBindTag.new 61tag1.bind('ButtonPress-1', proc{puts 'bind "ButtonPress-1" of tag1'}) 62tag1.bind('ButtonRelease-1', proc{puts 'bind "ButtonRelease-1" of tag1'}) 63 64tag2 = TkBindTag.new 65tag2.bind('ButtonPress-1', 66 proc{ 67 puts 'bind "ButtonPress-1" of tag2' 68 puts 'call Tk.callback_continue' 69 Tk.callback_continue 70 puts 'never see this message' 71 }) 72tag2.bind('ButtonRelease-1', 73 proc{ 74 puts 'bind "ButtonRelease-1" of tag2' 75 puts 'call Tk.callback_continue' 76 Tk.callback_continue 77 puts 'never see this message' 78 }) 79 80tag3 = TkBindTag.new 81tag3.bind('ButtonPress-1', 82 proc{ 83 puts 'bind "ButtonPress-1" of tag3' 84 puts 'call Tk.callback_break' 85 Tk.callback_break 86 puts 'never see this message' 87 }) 88tag3.bind('ButtonRelease-1', 89 proc{ 90 puts 'bind "ButtonRelease-1" of tag3' 91 puts 'call Tk.callback_break' 92 Tk.callback_break 93 puts 'never see this message' 94 }) 95 96# set bindtags 97p b1.bindtags 98 99tags = b2.bindtags 100tags[2,0] = tag1 101tags[0,0] = tag1 102b2.bindtags(tags) 103p b2.bindtags 104 105tags = b3.bindtags 106tags[2,0] = tag2 107tags[0,0] = tag2 108b3.bindtags(tags) 109p b3.bindtags 110 111tags = b4.bindtags 112tags[2,0] = tag3 113tags[0,0] = tag3 114b4.bindtags(tags) 115p b4.bindtags 116 117b5.bindtags([tag1, TkButton, tag2, b5]) 118 119# create button to set button class binding 120TkButton.new(:text=>'set binding to TkButton class', 121 :command=>proc{ 122 puts 'call "set_class_bind"' 123 set_class_bind 124 }).pack(:pady=>7) 125 126# start event-loop 127Tk.mainloop 128