# Ruby tk drawing a line

**URL:** <https://www.ruby-forum.com/t/ruby-tk-drawing-a-line/117023>\
**Category:** Ruby\
**Created:** [October 12, 2007, 5:52am UTC](https://www.ruby-forum.com/t/ruby-tk-drawing-a-line/117023 "2007-10-12T05:52:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ed\_Redman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b4bc9f/32.png) [@Ed\_Redman](https://www.ruby-forum.com/u/Ed_Redman)\
**Post date:** [October 12, 2007, 5:52am UTC](https://www.ruby-forum.com/t/ruby-tk-drawing-a-line/117023/1 "2007-10-12T05:52:49Z")

</div>

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 = TkcLine.new(canvas, $startx, $starty, $endx, $endy)
  $a.width option.value
  $a.fill "#{color}"
  })

```

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

---

<div class="post-metadata">

**Author:** ![Ed\_Redman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b4bc9f/32.png) [@Ed\_Redman](https://www.ruby-forum.com/u/Ed_Redman)\
**Post date:** [October 12, 2007, 6:31am UTC](https://www.ruby-forum.com/t/ruby-tk-drawing-a-line/117023/2 "2007-10-12T06:31:58Z")

</div>

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  
try

$a.dash “”

or

$a.dash nil

See

[http://www.tcl.tk/man/tcl8.4/TkCmd/canvas.htm#M27](http://www.tcl.tk/man/tcl8.4/TkCmd/canvas.htm#M27)

for details.

---

<div class="post-metadata">

**Author:** ![Ed\_Redman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b4bc9f/32.png) [@Ed\_Redman](https://www.ruby-forum.com/u/Ed_Redman)\
**Post date:** [October 12, 2007, 6:43am UTC](https://www.ruby-forum.com/t/ruby-tk-drawing-a-line/117023/3 "2007-10-12T06:43:39Z")

</div>

From: Ed Redman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: ruby tk drawing a line  
Date: Fri, 12 Oct 2007 12:50:10 +0900  
Message-ID: [k7CPi.10981$G25.2545@edtnps89](mailto:k7CPi.10981$G25.2545@edtnps89)

> canvas.bind(‘B1-Motion’, proc { |e|  
> $endx = e.x  
> $endy = e.y
> 
> ```
> $a = TkcLine.new(canvas, $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 = TkCanvas.new(:relief=\>:ridge, :borderwidth=\>3).pack

f = TkFrame.new.pack(:fill=\>:x)

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

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

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

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

## Tk.mainloop

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).
