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.
<code>
#! /usr/bin/ruby
# Sample code from Programing Ruby, page 259
# Modified: Morton Goldberg
# 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
</code>
You might wish to check out the original pig latin example at
Programming Ruby: The Pragmatic Programmer's Guide
Regards, Morton
···
On Oct 7, 2007, at 3:41 PM, Erik Boling wrote:
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.