rtk’rs-
this took me a bit too long to do this, so perhaps you’ll find it useful -
it’s pretty basic but seems to do the job: a generic scrollable frame -
···
tkscrollframe.rb : a sample of Scrollable TkComposite TkFrame
require ‘tk’
class TkScrollFrame < TkFrame
#{{{
include TkComposite
def initialize_composite(keys={})
munge keys
keys = _symbolkey2str(keys)
create scrollbars
@v_scroll = TkScrollbar.new @frame, :orient=>'vertical'
@h_scroll = TkScrollbar.new @frame, :orient=>'horizontal'
create a canvas - frame - window combo to scroll
@c_frame = TkCanvas.new @frame
@f_frame = TkFrame.new @c_frame
@w_frame = TkcWindow.new @c_frame, 0, 0, :window=>@f_frame, :anchor=>'nw'
assign scrollbars
@c_frame.xscrollbar(@h_scroll)
@c_frame.yscrollbar(@v_scroll)
#@h_scroll.assign @c_frame
#@v_scroll.assign @c_frame
set up scrollregion to always be frame’s extent
set_scrollregion
@f_frame.bind('Configure', proc{ set_scrollregion })
allignment
TkGrid.rowconfigure(@frame, 0, 'weight'=>1, 'minsize'=>0)
TkGrid.columnconfigure(@frame, 0, 'weight'=>1, 'minsize'=>0)
@c_frame.grid('row'=>0, 'column'=>0, 'sticky'=>'news')
@h_scroll.grid('row'=>1, 'column'=>0, 'sticky'=>'ew')
@v_scroll.grid('row'=>0, 'column'=>1, 'sticky'=>'ns')
set up @frame
configure keys unless keys.empty?
end
private :initialize_composite
def set_scrollregion w=@f_frame.winfo_reqwidth, h=@f_frame.winfo_reqheight
@c_frame.scrollregion [0, 0, w, h]
end
use these to get a handle on frame to put widgets in
attr :f_frame
alias scrollframe f_frame
#}}}
end
################################################
demo
################################################
if FILE == $0
#{{{
root frame
r = TkRoot.new
note that the parent of all widgets bound for scrollframe MUST be f
sf = TkScrollFrame.new r
f = sf.scrollframe
set up first frame
frame0 = TkFrame.new f
l = TkLabel.new frame0, :text => ‘foobar’
l.pack
set up second frame
frame1 = TkFrame.new f
42.times do |i|
42.times do |j|
text = ‘%s, %s’ % [i, j]
l = TkLabel.new frame1, :text=>text, :relief=>‘raised’, :borderwidth=>1
l.grid :row=>i, :column=>j, :sticky=>‘news’
end
end
set up botton which toggles view/scrolls between frames
b = TkButton.new r,
:text => ‘press-me’,
:command => proc {
@show ||= ‘frame0’
if @show == ‘frame0’
frame1.pack_forget
frame0.pack
@show = ‘frame1’
else
frame0.pack_forget
frame1.pack
@show = ‘frame0’
end
}
main
b.pack :expand=>true, :fill=>‘x’
sf.pack :expand=>true, :fill=>‘both’
Tk.mainloop
#}}}
end
-a
ATTN: please update your address books with address below!
===============================================================================
EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
STP :: Solar-Terrestrial Physics Data | NCEI
NGDC :: http://www.ngdc.noaa.gov/
NESDIS :: http://www.nesdis.noaa.gov/
NOAA :: http://www.noaa.gov/
US DOC :: http://www.commerce.gov/The difference between art and science is that science is what we
understand well enough to explain to a computer.
Art is everything else.
– Donald Knuth, “Discover”/bin/sh -c ‘for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done’
===============================================================================