# Tk Layout help please

**URL:** <https://www.ruby-forum.com/t/tk-layout-help-please/73202>\
**Category:** Ruby\
**Created:** [October 22, 2006, 8:39pm UTC](https://www.ruby-forum.com/t/tk-layout-help-please/73202 "2006-10-22T20:39:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohammad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mohammad](https://www.ruby-forum.com/u/mohammad)\
**Post date:** [October 22, 2006, 8:39pm UTC](https://www.ruby-forum.com/t/tk-layout-help-please/73202/1 "2006-10-22T20:39:38Z")

</div>

okay this is an example of what I want it to look like:

> **[Yahoo | Mail, Weather, Search, Politics, News, Finance, Sports & Videos](https://www.yahoo.com)**
>
> Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!

I want to be able to have them edit items and their prices, it saves it  
into another txt file for another program, so I want them to be able to  
edit the items and prices the way the file saves is:

item  
price  
item  
price  
…

I currently don’t know how to do tabels or anything like that so if  
someone could help me out on that.

---

<div class="post-metadata">

**Author:** ![mohammad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mohammad](https://www.ruby-forum.com/u/mohammad)\
**Post date:** [October 23, 2006, 2:01am UTC](https://www.ruby-forum.com/t/tk-layout-help-please/73202/2 "2006-10-23T02:01:46Z")

</div>

On Oct 22, 2006, at 2:39 PM, Mohammad — wrote:

> price  
> item  
> price  
> …
> 
> I currently don’t know how to do tabels or anything like that so if  
> someone could help me out on that.

Implementing a table is not such an easy thing to do if you are new  
to Ruby/Tk. It might be better to use a pre-existing table widget  
rather than try to code one yourself.

There is a table widget in /usr/lib/ruby/1.8/tkextlib/tktable.rb (on  
Mac OS X – path might be somewhat different path on other platform).  
There are examples of using this widget at

\<[http://www.ruby-lang.org/cgibin/cvsweb.cgi/ruby/ext/tk/sample/](http://www.ruby-lang.org/cgibin/cvsweb.cgi/ruby/ext/tk/sample/)  
tkextlib/tktable/\>

I have not used this widget myself, so I can’t give you any tips on  
how to use it. Sorry.

Regards, Morton

---

<div class="post-metadata">

**Author:** ![mohammad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mohammad](https://www.ruby-forum.com/u/mohammad)\
**Post date:** [October 23, 2006, 12:18pm UTC](https://www.ruby-forum.com/t/tk-layout-help-please/73202/3 "2006-10-23T12:18:43Z")

</div>

From: Mohammad — [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Tk Layout help please  
Date: Mon, 23 Oct 2006 03:39:41 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> okay this is an example of what I want it to look like:  
> [Yahoo | Mail, Weather, Search, Politics, News, Finance, Sports & Videos](http://www.geocities.com/name_here44/tkdream.bmp)

## How about this?

require ‘tk’

class Editable\_TkListbox \< TkListbox  
def \_ebox\_placer(coord\_y)  
idx = self.nearest(coord\_y)  
x, y, w, h = self.bbox(idx)  
@ebox.place(:x =\> 0, :relwidth =\> 1.0,  
:y =\> y - self.selectborderwidth,  
:height =\> h + 2 \* self.selectborderwidth)  
@ebox.pos = idx  
@ebox.value = self.listvariable.list[idx]  
@ebox.focus  
end  
private :\_ebox\_placer

def initialize(\*args)  
super(\*args)

```
unless self.listvariable
  self.listvariable = TkVariable.new(self.get(0, :end))
end

@ebox = TkEntry.new(self){
  @pos = -1
  def self.pos; @pos; end
  def self.pos=(idx); @pos = idx; end
}

@ebox.bind('Return'){
  list = self.listvariable.list
  list[@ebox.pos] = @ebox.value
  self.listvariable.value = list
  @ebox.place_forget
  @ebox.pos = -1
}

@ebox.bind('Escape'){
  @ebox.place_forget
  @ebox.pos = -1
}

self.bind('Double-1', '%y'){|y| _ebox_placer(y) }

```

end  
end

if $0 == **FILE**  
scr = TkScrollbar.new.pack(:side=\>:right, :fill=\>:y)

lbox1 = Editable\_TkListbox.new.pack(:side=\>:left)  
lbox2 = Editable\_TkListbox.new.pack(:side=\>:left)

scr.assign(lbox1, lbox2)

lbox1.insert(:end, \*%w(a b c d e f g h i j k l m n))  
lbox2.insert(:end, 0,1,2,3,4,5,6,7,8,9,0,1,2,3)

Tk.mainloop  
end
