TkVariable Array?

K

Kujawa, Greg

I am attempting to create an array of TkScale widgets to be used for scoring
a survey. This is because there might not always be a fixed number of
scoring responses and I wanted my script to be flexible. The values of the
TkScale widgets need to be stored for future reference. Therefore I assumed
the need to associate them with TkVariable objects. Here's my code:

# Create a scale widget for scoring each category
@@scales=Array.new
@@scores=TkVariable.new.to_a
@@category.each_index { |i|
@@scales=
TkScale.new(@@survey) {
from 0
to 9
length 400
orient 'horizontal'
tickinterval 1
variable @@scores
}
}

The problem is when I try to return the values of the TkVariable array
(@@scores) all I get back is an empty pair of brackets. Any suggestions
about how I could fix this would be appreciated. I'm only about two weeks
into learning the Ruby language and don't know a lot of Tk, as I am just
googling for sample code to try to guess at this point.

Thanks!
 
H

Hidetoshi NAGAI

Hi,

From: "Kujawa, Greg" <[email protected]>
Subject: TkVariable Array?
Date: Fri, 19 Nov 2004 00:13:35 +0900
Message-ID: said:
# Create a scale widget for scoring each category
@@scales=Array.new
@@scores=TkVariable.new.to_a
@@category.each_index { |i|
@@scales=
TkScale.new(@@survey) {
from 0
to 9
length 400
orient 'horizontal'
tickinterval 1
variable @@scores
}
}

The problem is when I try to return the values of the TkVariable array
(@@scores) all I get back is an empty pair of brackets. Any suggestions
about how I could fix this would be appreciated. I'm only about two weeks
into learning the Ruby language and don't know a lot of Tk, as I am just
googling for sample code to try to guess at this point.


Tk widgets can access Tcl's variables only.
TkVariable instance is an object to bridge between Ruby and
Tcl's variable.
For your purpose, you must assign a TkVariable object to each
TkScale widget.
There are many way to do that.
For example

---<sample1>-----------------------------------------
@@scales=Array.new
@@scores=Array.new
@@category.each_index { |i|
@@scores = TkVariable.new
@@scales=
TkScale.new(@@survey) {
from 0
to 9
length 400
orient 'horizontal'
tickinterval 1
variable @@scores
}
}
-----------------------------------------------------

If you use Ruby 1.8.2, you can use a Tk's Array Varible by the
following way.

---<sample2>-----------------------------------------
@@scales=Array.new
@@scores=TkVariable.new_hash
@@category.each_index { |i|
@@scales=
TkScale.new(@@survey) {
from 0
to 9
length 400
orient 'horizontal'
tickinterval 1
variable @@scores.ref(i)
}
}
 

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
474,161
Messages
2,570,892
Members
47,430
Latest member
7dog123

Latest Threads

Top