2004-05-01 12:09:54 -04:00
|
|
|
#!/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{
|
2004-10-11 00:51:21 -04:00
|
|
|
@press = false
|
|
|
|
self.background(self.highlightbackground)
|
|
|
|
self.relief('raised')
|
|
|
|
})
|
2004-05-01 12:09:54 -04:00
|
|
|
|
|
|
|
self.bind('ButtonPress-1', proc{@press = true; self.relief('sunken')})
|
|
|
|
self.bind('ButtonRelease-1', proc{
|
2004-10-11 00:51:21 -04:00
|
|
|
self.relief('raised')
|
|
|
|
@command.call if @press && @command
|
|
|
|
@press = false
|
|
|
|
})
|
2004-05-01 12:09:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def command(cmd = Proc.new)
|
|
|
|
@command = cmd
|
|
|
|
end
|
|
|
|
|
|
|
|
def invoke
|
|
|
|
if @command
|
2009-03-05 22:56:38 -05:00
|
|
|
@command.call
|
2004-05-01 12:09:54 -04:00
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
TkLabel.new(:text=><<EOT).pack
|
|
|
|
This is a sample of 'event binding'.
|
|
|
|
The first button is a normal button widget.
|
2009-03-05 22:56:38 -05:00
|
|
|
And the second one is a normal label widget
|
2004-05-01 12:09:54 -04:00
|
|
|
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
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
TkButton.new(:text=>'normal Button widget',
|
2004-10-11 00:51:21 -04:00
|
|
|
:command=>proc{
|
|
|
|
puts 'button is clicked!!'
|
|
|
|
lbl.text 'button is clicked!!'
|
|
|
|
v.numeric += 1
|
|
|
|
}){
|
2004-05-01 12:09:54 -04:00
|
|
|
pack(:fill=>:x, :expand=>true)
|
|
|
|
}
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
Button_clone.new(:text=>'Label with Button binding',
|
2004-10-11 00:51:21 -04:00
|
|
|
:command=>proc{
|
|
|
|
puts 'label is clicked!!'
|
|
|
|
lbl.text 'label is clicked!!'
|
|
|
|
v.numeric += 1
|
|
|
|
}){
|
2004-05-01 12:09:54 -04:00
|
|
|
pack(:fill=>:x, :expand=>true)
|
|
|
|
}
|
|
|
|
|
|
|
|
Tk.mainloop
|