[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby TKentry

Erik Boling

10/7/2007 7:41:00 PM

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.
--
Posted via http://www.ruby-....

4 Answers

Paul Stickney

10/7/2007 8:58:00 PM

0

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/rub...e...)

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

Morton Goldberg

10/8/2007 2:18:00 AM

0

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.

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/e...

Regards, Morton

Erik Boling

10/13/2007 7:56:00 PM

0

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?
--
Posted via http://www.ruby-....

Morton Goldberg

10/13/2007 8:55:00 PM

0

On Oct 13, 2007, at 3:56 PM, Erik Boling 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