# Ruby/Tk

**URL:** <https://www.ruby-forum.com/t/ruby-tk/121716>\
**Category:** Ruby\
**Created:** [November 30, 2007, 6:04am UTC](https://www.ruby-forum.com/t/ruby-tk/121716 "2007-11-30T06:04:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@dust](https://www.ruby-forum.com/u/dust)\
**Post date:** [November 30, 2007, 6:04am UTC](https://www.ruby-forum.com/t/ruby-tk/121716/1 "2007-11-30T06:04:33Z")

</div>

```
I have been looking for a good ruby GUI, and TK seems to be easiest

```

for me. I am quite new to programing in general, so bare with me if i’m  
using the wrong “wordage” for things. I have been creating simple  
programs, i.e basic skills math tests. But i have gotten bored/ annoyed  
of cmd prompt. I was wondering does anyone know of any good tutorials  
on how to program w/ TK. I have found some, but they are all very basic  
and only go as far as explaining how to put buttons and text on the  
window. I was trying to get my GUI more _active_ like being able to  
output and input information. Perhaps there is a better choice for a  
Ruby GUI that someone might recommend that has more extensive easier  
tutorials?  
Thanks for the help :)!

---

<div class="post-metadata">

**Author:** ![dust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@dust](https://www.ruby-forum.com/u/dust)\
**Post date:** [November 30, 2007, 7:21am UTC](https://www.ruby-forum.com/t/ruby-tk/121716/2 "2007-11-30T07:21:13Z")

</div>

On Nov 30, 2007, at 12:04 AM, Erik B. wrote:

> and only go as far as explaining how to put buttons and text on the  
> window. I was trying to get my GUI more _active_ like being able to  
> output and input information. Perhaps there is a better choice for a  
> Ruby GUI that someone might recommend that has more extensive easier  
> tutorials?

AFAIK there isn’t anything much more than introductory material  
available in English on-line for Ruby/Tk. It’s really too bad because  
Ruby/Tk provides a broad and deep range of GUI capabilities. After I  
(rather quickly) reached the limitations of the available tutorials,  
I turned to

\<[http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby\_1\_8/ext/tk/](http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ext/tk/)  
sample/\>

This is not a tutorial but a repository of example code. I have  
learned much from studying these examples. I recommend you take a  
look at them. Also, if you have a question about a specific Ruby/Tk  
element you can post it here. I’ll answer it if I can. But more to  
the point, NAGAI Hidetoshi (the implementor/maintainer of Ruby/Tk)  
often answers such question, and his answers are always illuminating.  
I have learned a great deal from them.

Regards, Morton

---

<div class="post-metadata">

**Author:** ![dust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@dust](https://www.ruby-forum.com/u/dust)\
**Post date:** [December 1, 2007, 6:31am UTC](https://www.ruby-forum.com/t/ruby-tk/121716/3 "2007-12-01T06:31:38Z")

</div>

Tk started off as the graphical tool kit for a scripting language  
called Tcl/Tk. I have a strong feeling that once you grasp the basics  
of Ruby/Tk you can get some good ideas about how to use it by searching  
for stuff that describes what you want to do at  
[http://wiki.tcl.tk](http://wiki.tcl.tk) (the Tcl/Tk) wiki.

You’ll need to translate the Tcl code that you see there to the  
Ruby/Tk idioms but Tcl/Tk is pretty easy to read most of the time.

---

<div class="post-metadata">

**Author:** ![dust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@dust](https://www.ruby-forum.com/u/dust)\
**Post date:** [December 2, 2007, 12:03am UTC](https://www.ruby-forum.com/t/ruby-tk/121716/4 "2007-12-02T00:03:45Z")

</div>

Ron F. wrote:

> You’ll need to translate the Tcl code that you see there to the  
> Ruby/Tk idioms but Tcl/Tk is pretty easy to read most of the time.

I’m not sure how to start off translating all this code justt see a bit  
might help, anyone know how to trans. this?

---

<div class="post-metadata">

**Author:** ![dust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@dust](https://www.ruby-forum.com/u/dust)\
**Post date:** [December 2, 2007, 12:04am UTC](https://www.ruby-forum.com/t/ruby-tk/121716/5 "2007-12-02T00:04:49Z")

</div>

Erik B. wrote:

> Ron F. wrote:
> 
> > You’ll need to translate the Tcl code that you see there to the  
> > Ruby/Tk idioms but Tcl/Tk is pretty easy to read most of the time.
> 
> I’m not sure how to start off translating all this code justt see a bit  
> might help, anyone know how to trans. this?

sorry hit enter =(! ok, this is the code!..: #!  
/usr/local/bin/wish8.1 button .b -text 0 -command {.b config -text [expr  
[.b cget -text]+1]}  
pack .b ;#RS

---

<div class="post-metadata">

**Author:** ![dust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@dust](https://www.ruby-forum.com/u/dust)\
**Post date:** [December 6, 2007, 7:54am UTC](https://www.ruby-forum.com/t/ruby-tk/121716/6 "2007-12-06T07:54:28Z")

</div>

From: Erik B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: Ruby/Tk  
Date: Sun, 2 Dec 2007 08:04:46 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > I’m not sure how to start off translating all this code justt see a bit  
> > might help, anyone know how to trans. this?
> 
> sorry hit enter =(! ok, this is the code!..: #!  
> /usr/local/bin/wish8.1 button .b -text 0 -command {.b config -text [expr  
> [.b cget -text]+1]}  
> pack .b ;#RS

Follwoings are some of the examples.  
Please see also  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/138653](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/138653).

## -----\<ex.1\>--------------------------------------------- b = TkButton.new(:text=\>‘0’, :command=\>proc{b.text = b.text.to\_i + 1}).pack

## -----\<ex.2\>-------------------------------------------- b = TkButton.new(‘text’=\>‘0’, ‘command’=\>proc{b.text = b.text.to\_i + 1}).pack

## -----\<ex.3\>--------------------------------------------- b = TkButton.new(:text=\>‘0’, :command=\>proc{b.text(b.text.to\_i + 1)}).pack

## -----\<ex.4\>--------------------------------------------- b = TkButton.new(:text=\>‘0’, :command=\>proc{b[:text] = b.text.to\_i + 1}).pack

## -----\<ex.5\>--------------------------------------------- b = TkButton.new(:text=\>‘0’, :command=\>proc{b[‘text’] = b.text.to\_i + 1}).pack

## -----\<ex.6\>--------------------------------------------- TkButton.new(:text=\>‘0’){|b| command{b.text = b.text.to\_i + 1} }.pack

## -----\<ex.7\>--------------------------------------------- TkButton.new(:text=\>‘0’){ command{self.text = self.text.to\_i + 1} }.pack

## -----\<ex.8\>--------------------------------------------- TkButton.new(:text=\>‘0’){ command{text(text.to\_i + 1)} }.pack

## -----\<ex.9\>--------------------------------------------- TkButton.new{ text 0 command{text(text.to\_i + 1)} pack }

## -----\<ex.10\>-------------------------------------------- cnt = 0 b = TkButton.new(:text=\>cnt, :command=\>proc{b.text = (cnt += 1)}).pack

## -----\<ex.11\>-------------------------------------------- TkButton.new{ cnt = 0 text cnt command{text(cnt+=1)} pack }

-----\<ex.12\>---------------------------------------------  
v = TkVariable.new(0)  
TkButton.new(:textvariable=\>v, :command=\>proc{v.numeric += 1}).pack
