2004-07-14 21:18:57 -04:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
##
|
|
|
|
## command.rb
|
|
|
|
##
|
|
|
|
## This demo shows the use of the table widget's -command options
|
|
|
|
##
|
|
|
|
## ( based on 'command.tcl' included source archive of tktable extension )
|
|
|
|
##
|
|
|
|
require 'tk'
|
|
|
|
require 'tkextlib/tktable'
|
|
|
|
|
|
|
|
# create the table
|
|
|
|
data = TkVariable.new_hash
|
|
|
|
rows = 10
|
|
|
|
cols = 10
|
|
|
|
|
|
|
|
# fill table variable
|
|
|
|
((-(rows))..rows).each{|x|
|
|
|
|
((-(cols))..cols).each{|y|
|
|
|
|
data[x,y] = "#{x} x #{y}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lbl = TkLabel.new(:text=>"TkTable :command Example")
|
|
|
|
cur_var = TkVariable.new
|
|
|
|
current = TkLabel.new(:textvariable=>cur_var, :width=>5)
|
|
|
|
ent_var = TkVariable.new
|
|
|
|
entry = TkEntry.new(:textvariable=>ent_var)
|
|
|
|
|
|
|
|
table = Tk::TkTable.new(:rows=>rows, :cols=>cols,
|
2004-10-11 00:51:21 -04:00
|
|
|
:command=>[proc{|mode, cell, val|
|
|
|
|
if (mode == :w)
|
|
|
|
data[cell] = val
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
data[cell] # exist
|
|
|
|
rescue
|
|
|
|
'' # not exist
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}, '%i %C %s'],
|
|
|
|
:width=>6, :height=>6,
|
|
|
|
:titlerows=>1, :titlecols=>1,
|
|
|
|
:roworigin=>-1, :colorigin=>-1,
|
|
|
|
:rowstretchmode=>:last, :colstretchmode=>:last,
|
|
|
|
:rowtagcommand=>proc{|row|
|
|
|
|
row = Integer(row)
|
2005-03-30 03:44:19 -05:00
|
|
|
(row>0 && row%2 == 1)? 'OddRow': ''
|
2004-10-11 00:51:21 -04:00
|
|
|
},
|
|
|
|
:coltagcommand=>proc{|col|
|
|
|
|
col = Integer(col)
|
2005-03-30 03:44:19 -05:00
|
|
|
(col>0 && col%2 == 1)? 'OddCol': ''
|
2004-10-11 00:51:21 -04:00
|
|
|
},
|
|
|
|
:selectmode=>:extended, :flashmode=>true,
|
|
|
|
:rowstretch=>:unset, :colstretch=>:unset,
|
|
|
|
:browsecommand=>[proc{|w, s|
|
|
|
|
cur_var.value = s
|
|
|
|
ent_var.value = w.get(s)
|
|
|
|
}, '%W %S'],
|
|
|
|
:validate=>true,
|
|
|
|
:validatecommand=>proc{|e|
|
|
|
|
ent_var.value = e.new_value; true
|
|
|
|
})
|
2004-07-14 21:18:57 -04:00
|
|
|
=begin
|
2004-10-11 00:51:21 -04:00
|
|
|
:validatecommand=>[
|
|
|
|
proc{|s|
|
|
|
|
ent_var.value = s; true
|
|
|
|
}, '%S'])
|
2004-07-14 21:18:57 -04:00
|
|
|
=end
|
|
|
|
|
|
|
|
sx = table.xscrollbar(TkScrollbar.new)
|
|
|
|
sy = table.yscrollbar(TkScrollbar.new)
|
|
|
|
|
|
|
|
entry.bind('Return', proc{|w| table.curvalue = w.value}, '%W')
|
|
|
|
|
|
|
|
Tk.grid(lbl, '-', '-', :sticky=>:ew)
|
|
|
|
Tk.grid(current, entry, '-', :sticky=>:ew)
|
|
|
|
Tk.grid(table, '-', sy, :sticky=>:news)
|
|
|
|
Tk.grid(sx, '-', :sticky=>:ew)
|
|
|
|
|
|
|
|
Tk.root.grid_columnconfig(1, :weight=>1)
|
|
|
|
Tk.root.grid_rowconfig(2, :weight=>1)
|
|
|
|
|
|
|
|
table.tag_configure('OddRow', :bg=>'orange', :fg=>'purple')
|
|
|
|
table.tag_configure('OddCol', :bg=>'brown', :fg=>'pink')
|
|
|
|
|
|
|
|
puts "Table is #{table.path}"
|
|
|
|
|
|
|
|
Tk.mainloop
|