Ruby TKentry

E

Erik Boling

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.
 
P

Paul Stickney

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)

I would start learning Ruby/Tk by following the "Course" at:
http://members.chello.nl/k.vangelder/ruby/learntk/
 
M

Morton Goldberg

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.

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

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

Regards, Morton
 
E

Erik Boling

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?
 
M

Morton Goldberg

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top