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