# Ruby TKentry

**URL:** <https://www.ruby-forum.com/t/ruby-tkentry/116603>\
**Category:** Ruby\
**Created:** [October 7, 2007, 9:41pm UTC](https://www.ruby-forum.com/t/ruby-tkentry/116603 "2007-10-07T21:41:18Z")\
**Posts on this page:** 5\
**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:** [October 7, 2007, 9:41pm UTC](https://www.ruby-forum.com/t/ruby-tkentry/116603/1 "2007-10-07T21:41:18Z")

</div>

So, i have been programming for about a month or so and i decided to try  
out a GUI for some of my programs. I just started using ruby tk. I found  
some tutroials, but there not very detailed.  
I have been fooling around with it a bit, but i cant get the TKentry  
class to work.Supposedly according to the tutorials, it is used for  
getting input, but i dont know how to store the data.  
heres an example:  
puts ‘how old are you?’  
age = gets.chomp.

for TK its:  
entry = TkEntry.new(root).pack # equivalent to gets _I’m guessing_

but im not sure how to store the input…?  
also i have noticed things like sleep 3 don’t work with TK?

If any one knows any good tutorials or just has the answer to my  
questions that would be much appreciated

ps, sorry if im using the wrong _wordage_ for things, Im quite new to  
programming.

---

<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:** [October 7, 2007, 10:58pm UTC](https://www.ruby-forum.com/t/ruby-tkentry/116603/2 "2007-10-07T22:58:31Z")

</div>

When you do GUI programming you move away from a sequentially  
programming model to an event-driven model.

a = gets

In the basic form, and making a few assumptions, this reads a line  
from STDIN. It is a blocking method call–after the actual line will  
be returned.

entry = TkEntry.new(root).pack

This just creates a new Tk widget (in this case, a place to enter some  
text) and returns immediately (it is also packed in the layout). To  
get the value of the user input later on you likely want to setup an  
event. (see:  
[http://members.chello.nl/k.vangelder/ruby/learntk/events.html](http://members.chello.nl/k.vangelder/ruby/learntk/events.html))

I would start learning Ruby/Tk by following the “Course” at:  
[http://members.chello.nl/k.vangelder/ruby/learntk/](http://members.chello.nl/k.vangelder/ruby/learntk/)

---

<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:** [October 8, 2007, 4:19am UTC](https://www.ruby-forum.com/t/ruby-tkentry/116603/3 "2007-10-08T04:19:15Z")

</div>

On Oct 7, 2007, at 3:41 PM, Erik B. wrote:

> age = gets.chomp.  
> ps, sorry if im using the wrong _wordage_ for things, Im quite new to  
> programming.

A TkEntry widget will store the input (the text the user enters) in  
it’s value attribute. Given

```
entry = TkEntry.new(root).pack

```

you can get it with

```
current_value = entry.value

```

and set it with

```
entry.value = new_value

```

Here is an example based on one in the Pick Axe book. The original  
used a TkVariable to store the text, but I have modified it to work  
the way I have described above.

`
#! /usr/bin/ruby
Sample code from Programing Ruby, page 259
Modified: Morton G.
Date: May 16, 2006
require ‘tk’
class PigBox
def pig(word)
leading_cap = word =~ /^[A-Z]/
word.downcase!
res = case word
when /^[aeiouy]/ then word + “way”
when /^([^aeiouy]+)(.*)/ then $2 + $1 + “ay”
else word
end
leading_cap ? res.capitalize : res
end
```
# This reads the TkEntry's text, modifies it, and writes it back
# out to the widget.
def show_pig
   @entry.value = @entry.value.split.collect{|w| pig(w)}.join(" ")
end

# Commented-out code allowed me to determine attractive geometry
# values for root window.
def pig_exit
   # puts Tk.root.winfo_geometry
   exit
end

def initialize
   # (me = self) is trick to make PigBox object visible within

```
blocks
# supplied to widget constructors.
me = self
xy_pad = {‘padx’=>15, ‘pady’=>10}
# Want light blue background for all elements except buttons.
# Color names and rgb values are given in ‘colors (n)’ man page.
bg = {‘background’=>‘light blue’}
# This is needed to make butons blend into background.
hbg = {‘highlightbackground’=>‘light blue’}
root = TkRoot.new(bg) {
title “Pig Latin”
geometry ‘450x100+900+50’
resizable false, false
}
# Lay out label and entry box horizontally in upper frame.
top = TkFrame.new(root, bg)
h = xy_pad.dup
h[‘side’] = ‘left’
TkLabel.new(top, bg) {
text ‘Enter Text:’
pack(h)
}
h[‘side’] = ‘right’
h[‘fill’] = ‘x’
h[‘expand’] = true
@entry = TkEntry.new(top) {
font “System 14”
pack(h)
}
top.pack(‘fill’=>‘both’, ‘side’=>‘top’)
# Lay out Pig It and Exit buttons horizontally on right side of
# lower frame.
bottom = TkFrame.new(root, bg)
h = xy_pad.dup
h[‘side’] = ‘right’
exit_btn = TkButton.new(bottom, hbg) {
text ‘Exit’
command { me.pig_exit }
pack(h)
}
pig_btn = TkButton.new(bottom, hbg) {
text ‘Pig It’
command { me.show_pig }
pack(h)
}
bottom.pack(‘fill’=>‘both’, ‘side’=>‘bottom’)
end
end
PigBox.new
Tk.mainloop
`

You might wish to check out the original pig latin example at

```
http://www.ruby-doc.org/docs/ProgrammingRuby/html/ext_tk.html

```

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:** [October 13, 2007, 9:56pm UTC](https://www.ruby-forum.com/t/ruby-tkentry/116603/4 "2007-10-13T21:56:23Z")

</div>

Alright i see now. But for example when it outputs a value, it outputs  
it into cmd, i was wondering if it was possible to output to the actual  
window?

---

<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:** [October 13, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/ruby-tkentry/116603/5 "2007-10-13T22:56:25Z")

</div>

On Oct 13, 2007, at 3:56 PM, Erik B. wrote:

> Alright i see now. But for example when it outputs a value, it outputs  
> it into cmd, i was wondering if it was possible to output to the  
> actual  
> window?

This is not clear to me, so i can’t see how to help. Can you explain  
what you want to do in more detail? Perhaps it would be a good idea  
to post your code.

Regards, Morton
