Tkscrollframe.rb

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’
===============================================================================

Hi,

···

From: Ara.T.Howard@noaa.gov
Subject: tkscrollframe.rb
Date: Wed, 7 Jan 2004 10:36:42 +0900
Message-ID: Pine.LNX.4.44.0401061700240.2595-100000@fattire.ngdc.noaa.gov

note that the parent of all widgets bound for scrollframe MUST be f

sf = TkScrollFrame.new r
f = sf.scrollframe

If you set @path instance variable, you don’t need the access method
for the frame widget. To decide the target widget name of method calls,
@path variable is used. And geometry managers use @epath.

— tkscrollframe.rb.orig 2004-01-07 12:37:29.000000000 +0900
+++ tkscrollframe.rb 2004-01-07 12:38:16.000000000 +0900
@@ -21,6 +21,8 @@
@f_frame = TkFrame.new @c_frame
@w_frame = TkcWindow.new @c_frame, 0, 0, :window=>@f_frame, :anchor=>‘nw’

  • @path = @f_frame.path
  • assign scrollbars

    @c_frame.xscrollbar(@h_scroll)
    @c_frame.yscrollbar(@v_scroll)
    @@ -47,10 +49,6 @@
    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

@@ -65,8 +63,7 @@
r = TkRoot.new

note that the parent of all widgets bound for scrollframe MUST be f

  • sf = TkScrollFrame.new r
  • f = sf.scrollframe
  • f = TkScrollFrame.new r

set up first frame

frame0 = TkFrame.new f
@@ -102,7 +99,7 @@

main

b.pack :expand=>true, :fill=>‘x’

  • sf.pack :expand=>true, :fill=>‘both’
  • f.pack :expand=>true, :fill=>‘both’
    Tk.mainloop

#}}}


Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)