diff options
| author | (no author) <(no author)@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2004-05-01 16:09:55 +0000 |
|---|---|---|
| committer | (no author) <(no author)@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2004-05-01 16:09:55 +0000 |
| commit | f15eecbbb6b9a1c5d8b22155a1f86d1567970353 (patch) | |
| tree | 5f4e27703ff8e79e7b5e05afeda3d9643098075d /ext/tk/sample/binding_sample.rb | |
| parent | 4ae99a8896f7ac00dec7e2c86183d4015a3a8477 (diff) | |
| download | ruby-f15eecbbb6b9a1c5d8b22155a1f86d1567970353.tar.gz ruby-f15eecbbb6b9a1c5d8b22155a1f86d1567970353.tar.xz ruby-f15eecbbb6b9a1c5d8b22155a1f86d1567970353.zip | |
This commit was manufactured by cvs2svn to create branch 'ruby_1_8'.
git-svn-id: http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_8@6238 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/tk/sample/binding_sample.rb')
| -rw-r--r-- | ext/tk/sample/binding_sample.rb | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/ext/tk/sample/binding_sample.rb b/ext/tk/sample/binding_sample.rb new file mode 100644 index 000000000..b98cd66a2 --- /dev/null +++ b/ext/tk/sample/binding_sample.rb @@ -0,0 +1,87 @@ +#!/usr/bin/env ruby + +require 'tk' + +class Button_clone < TkLabel + def initialize(*args) + @command = nil + + if args[-1].kind_of?(Hash) + keys = _symbolkey2str(args.pop) + @command = keys.delete('command') + + keys['highlightthickness'] = 1 unless keys.key?('highlightthickness') + keys['padx'] = '3m' unless keys.key?('padx') + keys['pady'] = '1m' unless keys.key?('pady') + keys['relief'] = 'raised' unless keys.key?('relief') + + args.push(keys) + end + + super(*args) + + @press = false + + self.bind('Enter', proc{self.background(self.activebackground)}) + self.bind('Leave', proc{ + @press = false + self.background(self.highlightbackground) + self.relief('raised') + }) + + self.bind('ButtonPress-1', proc{@press = true; self.relief('sunken')}) + self.bind('ButtonRelease-1', proc{ + self.relief('raised') + @command.call if @press && @command + @press = false + }) + end + + def command(cmd = Proc.new) + @command = cmd + end + + def invoke + if @command + @command.call + else + '' + end + end +end + +TkLabel.new(:text=><<EOT).pack +This is a sample of 'event binding'. +The first button is a normal button widget. +And the second one is a normal label widget +but with some bindings like a button widget. +EOT + +lbl = TkLabel.new(:foreground=>'red').pack(:pady=>3) + +v = TkVariable.new(0) + +TkFrame.new{|f| + TkLabel.new(f, :text=>'click count : ').pack(:side=>:left) + TkLabel.new(f, :textvariable=>v).pack(:side=>:left) +}.pack + +TkButton.new(:text=>'normal Button widget', + :command=>proc{ + puts 'button is clicked!!' + lbl.text 'button is clicked!!' + v.numeric += 1 + }){ + pack(:fill=>:x, :expand=>true) +} + +Button_clone.new(:text=>'Label with Button binding', + :command=>proc{ + puts 'label is clicked!!' + lbl.text 'label is clicked!!' + v.numeric += 1 + }){ + pack(:fill=>:x, :expand=>true) +} + +Tk.mainloop |
