Perl/Tk, CheckButtons, and modifying variables

I'm trying to use a series of CheckButtons, and all is going pretty
well.

However, I want to change the associated variable and have it reflected
in the CheckButton. I'm not sure of how to do this.

I searched through Google Groups and Google Web and found nothing.

Because of the number of CheckButtons in my case (300) they are split
up into 10 columns on screen in 10 different frames (which shouldn't
matter). The Checkbuttons are actually stored in a Ruby hash (based on
a text string - server name, in this case).

I created a series of associated TkVariables in a related hash, but how
do I set these variables? I've been just assigning a 1 (integer) but I
wonder.... does that change the hash value from a TkVariable to an
Integer?

I'm missing something - everything works except there is no reflection
in the display of the current status of the variables...

Something like
my_tk_checkbox_var = TkVariable.new
# create a checkbox assining its tkvariable to my_tk_checkbox_var
my_tk_checkbox_var.value = 1 # or maybe "1", i forget

That should check the checkbox.

···

On 7/14/05, David Douthitt <ssrat@mailbag.com> wrote:

I'm trying to use a series of CheckButtons, and all is going pretty
well.

However, I want to change the associated variable and have it reflected
in the CheckButton. I'm not sure of how to do this.

I searched through Google Groups and Google Web and found nothing.

Because of the number of CheckButtons in my case (300) they are split
up into 10 columns on screen in 10 different frames (which shouldn't
matter). The Checkbuttons are actually stored in a Ruby hash (based on
a text string - server name, in this case).

I created a series of associated TkVariables in a related hash, but how
do I set these variables? I've been just assigning a 1 (integer) but I
wonder.... does that change the hash value from a TkVariable to an
Integer?

I'm missing something - everything works except there is no reflection
in the display of the current status of the variables...

That is basically what I'm doing but it's not working. I'd put the
code up but it's longish for posting to Usenet. The basics boil down
to something like this:

$serverVariable["server"] = TkVariable.new
.....

TkButton.new(x) {
    ....
    command { $serverVariable["server"] = 1 }
}

It doesn't work right somehow...

I looked at "pack" and "configure" (after variable assignment) and
neither seems to do what I want.

I think I'm getting closer (at least) to understanding this. Instead
of:

$serverVariable["server"] = 1

....what I want is, in reality:

lvalue_of_what_is_pointed_to_by($serverVariable["server"]) = 1

....that is, this $serverVariable["server"] contains a TkVariable, and
after the assignment it contains a 1 instead. I want to set not the
array value, but the contents of the array value. A reference, if you
will.

But how? I'm still trying...

I think I'm getting closer (at least) to understanding this. Instead
of:

$serverVariable["server"] = 1

....what I want is, in reality:

lvalue_of_what_is_pointed_to_by($serverVariable["server"]) = 1

You mean,
$serverVariable["server"].value = 1
?

....that is, this $serverVariable["server"] contains a TkVariable, and
after the assignment it contains a 1 instead. I want to set not the
array value, but the contents of the array value. A reference, if you
will.

But how? I'm still trying...

Maybe this will help?

require 'tk'

$root = TkRoot.new
$var = TkVariable.new
$button = TkCheckButton.new $root, :variable => $var, :text => "Hello World"
$button.pack
# alternates the value of $var from one to zero every second.
TkTimer.start 1000, -1, proc { $var.value = $var.value == "0" ? 1 : 0 }
Tk.mainloop

···

On 7/14/05, David Douthitt <ssrat@mailbag.com> wrote:

That was it exactly. Thanks!

Now..... where was that documented again? :wink:

I didn't see anything in "The Ruby Way" and I didn't have my PickAxe
book with me. The PerlTk documents don't have a "Variable" or a
"TkVariable" listed anywhere.

Message-ID: <c715e6405071415434b702cb4@mail.gmail.com>

TkTimer.start 1000, -1, proc { $var.value = $var.value == "0" ? 1 : 0 }

Or, "TkTimer.start 1000, -1, proc { $var.bool ^= true }" :slight_smile:

···

From: Joe Van Dyk <joevandyk@gmail.com>
Subject: Re: Perl/Tk, CheckButtons, and modifying variables
Date: Fri, 15 Jul 2005 07:43:55 +0900
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

They don't?

http://perlhelp.web.cern.ch/PerlHelp/site/lib/Tk/Checkbutton.html

WIDGET-SPECIFIC OPTIONS (for checkbutton)
Name: variable
Class: Variable
Switch: -variable
    Specifies reference to a variable to set to indicate whether or
not this button is selected. Defaults to \$widget->{'Value'} member of
the widget's hash. In general perl variables are undef unless
specifically initialized which will not match either default -onvalue
or default -offvalue.

Also, you can always use
http://ruby.activeventure.com/programmingruby/book/ext_tk.html as a
substitute for the pickaxe paper copy.

But yeah, Tk documentation is fairly confusing. It's mostly because
Tk documentation itself is so confusing.

···

On 7/14/05, David Douthitt <ssrat@mailbag.com> wrote:

That was it exactly. Thanks!

Now..... where was that documented again? :wink:

I didn't see anything in "The Ruby Way" and I didn't have my PickAxe
book with me. The PerlTk documents don't have a "Variable" or a
"TkVariable" listed anywhere.

Message-ID: <1121382859.564956.239480@g44g2000cwa.googlegroups.com>

I didn't see anything in "The Ruby Way" and I didn't have my PickAxe
book with me. The PerlTk documents don't have a "Variable" or a
"TkVariable" listed anywhere.

Ruby/Tk accesses to Tcl's variables through TkVariable objects.

# Of course, "wait" and "trace" are available like as a Tcl's variable.

As you know, each value is kept as a string on a Tcl/Tk interpreter.
So, a Ruby's object which set to a Tcl's variable is converted to a
string (with TkUtil._get_eval_string() and so on).

Usually, use TkVariable#value/value= when the Tcl's variable is a
scalar type, or use TkVariable#[]/[]= when the variable is an array.
TkVariable.new creates a scalar variable.
However, TkVariable.new(<Hash object>) or TkVariable.new_hash creates
an array variable. For example,

···

From: "David Douthitt" <ssrat@mailbag.com>
Subject: Re: Perl/Tk, CheckButtons, and modifying variables
Date: Fri, 15 Jul 2005 08:15:51 +0900
-----------------------------------
v = TkVariable.new
v['a'] = 1 #=> raise exception!!
-----------------------------------
-----------------------------------
v = TkVariable.new(Hash.new)
v['a'] = 1
v['a'] #=> "1"
v.value #=> {"a"=>"1"}
-----------------------------------

The following is based on the latest version of Ruby/Tk.

To help to access the value of Tcl's variable, TkVariable has some
kind of access methods.
-------------------------------------------------
-----------+-----------+-----------
   type | get | set
===========+===========+===========
  String | value | value=
-----------+-----------+-----------
  Symbol | symbol | symbol=
-----------+-----------+-----------
  Numeric | numeric | numeric=
-----------+-----------+-----------
  Boolean | bool | bool=
-----------+-----------+-----------
  List | list | list=
-----------+-----------+-----------
  Num-List | numlist | numlist=
-----------+-----------+-----------
  Variable | variable | variable=
-----------+-----------+-----------
  Widget | window | window=
-----------+-----------+-----------
  Procedure | procedure | procedure=
-----------+-----------+-----------

* 'List' means that is a list of strings, and 'Num-List' means that
   is a list of Numeric values.

* 'Variable' means that its value is a Tcl's variable name.
   TkVariable#variable returns a TkVariable object.

* 'Widget' means that its value is a widget path.
   TkVariable#window returns a widget object.

* 'Procedure' means that its value is a script.
   If set a proc object to the variable, TkVariable#procedure returns
   a proc object.
-------------------------------------------------
Based on that,
-----------------------------------
v = TkVariable.new('no')
v.value #=> 'no'
v.bool #=> false
-----------------------------------
-----------------------------------
v = TkVariable.new(1)
v.numeric += 2
v.value #=> "3"
-----------------------------------

TkVariable object can have 'TYPE' of its value.
Default TYPE is nil. Then, TkVariable#value returns a string.
-----------------------------------
v = TkVariable.new(1)
v.default_value_type #=> nil
v.value #=> "1"
-----------------------------------
When set the TYPE, TkVariable#value returns a value of the TYPE.
-----------------------------------
v = TkVariable.new(1)
v.default_value_type = :numeric
v.value #=> 1
-----------------------------------

TkVariable has some operator methods.
For example,
-----------------------------------
v = TkVariable.new(1)
v + 2 #=> 3
-----------------------------------
-----------------------------------
v = TkVariable.new('asdf')
v + 'qwer' #=> 'asdfqwer'
-----------------------------------

Please see tk/variable.rb for other methods and ask if you have some
questions.
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)