Ruby tk drawing a line

I have a question concerning ruby/tk and drawing lines on a canvas.
I tried to convert from a tcl program.
Everything works fine except that the line when its thickness is large
appears to be a series of short line segments rather than a solid line.

I have binded the “1” mouse button to start the line with the following

canvas.bind( ‘1’, proc { |e|

                         $startx = e.x
                         $starty = e.y


and the motion of the mouse

canvas.bind(‘B1-Motion’, proc { |e|
$endx = e.x
$endy = e.y

  $a  =, $startx, $starty, $endx, $endy)
  $a.width option.value
  $a.fill "#{color}"

Can anyone see why this does not give me a solid line

Ed Redman wrote:

                         $starty = e.y
  $a.width option.value
  $a.fill "#{color}"

Can anyone see why this does not give me a solid line

Does the same thing happen without the fill param?

A TkcLine has a “dash” parameter, and that might be causing this. But it
would be surprising if the default is a dashed line. Anyway, you could

$a.dash “”


$a.dash nil


for details.

From: Ed Redman [email protected]
Subject: ruby tk drawing a line
Date: Fri, 12 Oct 2007 12:50:10 +0900
Message-ID: k7CPi.10981$G25.2545@edtnps89

canvas.bind(‘B1-Motion’, proc { |e|
$endx = e.x
$endy = e.y

  $a  =, $startx, $starty, $endx, $endy)

You write so many new lines at here.
Each short line is made at each ‘B1-Motion’ callback.

The following may be an example which you want.

require ‘tk’

line = nil
first = nil
last = nil

canvas =>:ridge, :borderwidth=>3).pack

f =>:x)

color = ‘black’, :text=>‘color’, :command=>proc{
color = Tk.chooseColor(:initialcolor=>color)
}).pack(:side=>:right, :padx=>[10,3])

option =, :from=>1, :to=>1000,
:width=>4).pack(:side=>:right, :padx=>0), :text=>‘width’).pack(:side=>:right, :padx=>[10,0])

canvas.bind(‘1’, ‘%x’, ‘%y’){|*first|
line =, first, first,
:width=>option.value, :fill=>color)

canvas.bind(‘B1-Motion’, ‘%x’, ‘%y’){|*last| line.coords(first, last)}


Ruby/Tk accepts all of the following coords pattern for a canvas item.

(1) x1, y1, x2, y2, x3, y3, …

(2) [x1, y1], [x2, y2], [x3, y3], …

(3) [ [x1, y1], [x2, y2], [x3, y3], … ]

In my example, I use the pattern (2).