Tk scrolled canvas

I can create a canvas like this.

root = TkRoot.new {title 'Diagram'}
canvas = TkCanvas.new(root)

I think I should be able to create a scrolled canvas like this, but it doesn't work.

scrolled = TkScrolledcanvas.new(root)

I've search the mailing list for the answer and can't find it.
What's the correct syntax?

Hi,

···

From: "R. Mark Volkmann" <mark@ociweb.com>
Subject: Tk scrolled canvas
Date: Sun, 5 Dec 2004 12:17:01 +0900
Message-ID: <05b101c4da78$e39092d0$0200a8c0@MarkDesktop>

I think I should be able to create a scrolled canvas like this, but it
doesn't work.

Please define the widget class which you want. :wink:
See sample scripts at ext/tk/samples directory of Ruby source archive.

If refer to ext/tk/samples/tktextframe.rb,
TkScrolledCanvas can be defined as such as the following.
---------------------------------------------------------------------
#!/usr/bin/env ruby
require 'tk'

class TkScrolledCanvas < TkCanvas
  include TkComposite

  def initialize_composite(keys={})
    @h_scr = TkScrollbar.new(@frame)
    @v_scr = TkScrollbar.new(@frame)

    @canvas = TkCanvas.new(@frame)
    @path = @canvas.path

    @canvas.xscrollbar(@h_scr)
    @canvas.yscrollbar(@v_scr)

    TkGrid.rowconfigure(@frame, 0, :weight=>1, :minsize=>0)
    TkGrid.columnconfigure(@frame, 0, :weight=>1, :minsize=>0)

    @canvas.grid(:row=>0, :column=>0, :sticky=>'news')
    @h_scr.grid(:row=>1, :column=>0, :sticky=>'ew')
    @v_scr.grid(:row=>0, :column=>1, :sticky=>'ns')

    delegate('DEFAULT', @canvas)
    delegate('background', @text, @h_scr, @v_scr)
    delegate('activeforeground', @h_scr, @v_scr)
    delegate('troughcolor', @h_scr, @v_scr)
    delegate('repeatdelay', @h_scr, @v_scr)
    delegate('repeatinterval', @h_scr, @v_scr)
    delegate('borderwidth', @frame)
    delegate('relief', @frame)

    delegate_alias('canvasborderwidth', 'borderwidth', @canvas)
    delegate_alias('canvasrelief', 'relief', @canvas)

    delegate_alias('scrollbarborderwidth', 'borderwidth', @h_scr, @v_scr)
    delegate_alias('scrollbarrelief', 'relief', @h_scr, @v_scr)

    configure(keys) unless keys.empty?
  end
end

c = TkScrolledCanvas.new(:scrollregion=>[0,0,500,400]).pack
TkcRectangle.new(c, [100,100], [300, 200])

Tk.mainloop
---------------------------------------------------------------------
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Hi,

From: "R. Mark Volkmann" <mark@ociweb.com>
Subject: Tk scrolled canvas
Date: Sun, 5 Dec 2004 12:17:01 +0900
Message-ID: <05b101c4da78$e39092d0$0200a8c0@MarkDesktop>

I think I should be able to create a scrolled canvas like this, but it
doesn't work.

Please define the widget class which you want. :wink:

I don't understand why I have to write my own TkScrolledCanvas class.
Why doesn't a scrolled canvas already exist?
It is discussed in the book "Learning Perl/Tk" on the first page of chapter 9.
The author says you can either create a plain "Canvas" or a "Scrolled" which is a scrollable Canvas.

···

----- Original Message ----- From: "Hidetoshi NAGAI" <nagai@ai.kyutech.ac.jp>
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Sent: Sunday, December 05, 2004 2:09 AM
Subject: Re: Tk scrolled canvas

See sample scripts at ext/tk/samples directory of Ruby source archive.

If refer to ext/tk/samples/tktextframe.rb,
TkScrolledCanvas can be defined as such as the following.
---------------------------------------------------------------------
#!/usr/bin/env ruby
require 'tk'

class TkScrolledCanvas < TkCanvas
include TkComposite

def initialize_composite(keys={})
   @h_scr = TkScrollbar.new(@frame)
   @v_scr = TkScrollbar.new(@frame)

   @canvas = TkCanvas.new(@frame)
   @path = @canvas.path

   @canvas.xscrollbar(@h_scr)
   @canvas.yscrollbar(@v_scr)

   TkGrid.rowconfigure(@frame, 0, :weight=>1, :minsize=>0)
   TkGrid.columnconfigure(@frame, 0, :weight=>1, :minsize=>0)

   @canvas.grid(:row=>0, :column=>0, :sticky=>'news')
   @h_scr.grid(:row=>1, :column=>0, :sticky=>'ew')
   @v_scr.grid(:row=>0, :column=>1, :sticky=>'ns')

   delegate('DEFAULT', @canvas)
   delegate('background', @text, @h_scr, @v_scr)
   delegate('activeforeground', @h_scr, @v_scr)
   delegate('troughcolor', @h_scr, @v_scr)
   delegate('repeatdelay', @h_scr, @v_scr)
   delegate('repeatinterval', @h_scr, @v_scr)
   delegate('borderwidth', @frame)
   delegate('relief', @frame)

   delegate_alias('canvasborderwidth', 'borderwidth', @canvas)
   delegate_alias('canvasrelief', 'relief', @canvas)

   delegate_alias('scrollbarborderwidth', 'borderwidth', @h_scr, @v_scr)
   delegate_alias('scrollbarrelief', 'relief', @h_scr, @v_scr)

   configure(keys) unless keys.empty?
end
end

c = TkScrolledCanvas.new(:scrollregion=>[0,0,500,400]).pack
TkcRectangle.new(c, [100,100], [300, 200])

Tk.mainloop
---------------------------------------------------------------------
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Hi,

···

From: "R. Mark Volkmann" <mark@ociweb.com>
Subject: Re: Tk scrolled canvas
Date: Mon, 6 Dec 2004 00:45:10 +0900
Message-ID: <064e01c4dae1$66d2bc00$0200a8c0@MarkDesktop>

I don't understand why I have to write my own TkScrolledCanvas class.
Why doesn't a scrolled canvas already exist?

Because a scrolled canvas is not a standard widget of Tcl/Tk. :slight_smile:

As I showed my last mail, it is easy to create a TkScrolledCanvas
widget class. However, it is not clear how much configurable the
class is required to add it to Ruby/Tk standard libraries.

Of course, we can allow full configuration of all part of the widget
by delegate_alias method and so on. But, is it realy right solution?
Maybe it only create many of configuration keys which have a very
long name and rare necessity. Possibly, it is not so easy to decide
the balance point. If a widget class is entried as a standard library,
it is a litte difficult to change specification of the widget.
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)