# Perl/Tk, CheckButtons, and modifying variables

**URL:** <https://rubytalk.org/t/perl-tk-checkbuttons-and-modifying-variables/19534>\
**Category:** ruby-talk\
**Created:** [14 July 2005 19:05 UTC](https://rubytalk.org/t/perl-tk-checkbuttons-and-modifying-variables/19534 "2005-07-14T19:05:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Douthitt1](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@David\_Douthitt1](https://rubytalk.org/u/David_Douthitt1)\
**Post date:** [14 July 2005 19:05 UTC](https://rubytalk.org/t/perl-tk-checkbuttons-and-modifying-variables/19534/1 "2005-07-14T19:05:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![J-Van](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@J-Van](https://rubytalk.org/u/J-Van)\
**Post date:** [14 July 2005 19:57 UTC](https://rubytalk.org/t/perl-tk-checkbuttons-and-modifying-variables/19534/2 "2005-07-14T19:57:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![David\_Douthitt1](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@David\_Douthitt1](https://rubytalk.org/u/David_Douthitt1)\
**Post date:** [14 July 2005 21:35 UTC](https://rubytalk.org/t/perl-tk-checkbuttons-and-modifying-variables/19534/3 "2005-07-14T21:35:51Z")

</div>

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) {  
&nbsp;&nbsp;&nbsp;&nbsp;....  
&nbsp;&nbsp;&nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![David\_Douthitt1](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@David\_Douthitt1](https://rubytalk.org/u/David_Douthitt1)\
**Post date:** [14 July 2005 22:00 UTC](https://rubytalk.org/t/perl-tk-checkbuttons-and-modifying-variables/19534/4 "2005-07-14T22:00:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![J-Van](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@J-Van](https://rubytalk.org/u/J-Van)\
**Post date:** [14 July 2005 22:43 UTC](https://rubytalk.org/t/perl-tk-checkbuttons-and-modifying-variables/19534/5 "2005-07-14T22:43:55Z")

</div>

> 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:

---

<div class="post-metadata">

**Author:** ![David\_Douthitt1](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@David\_Douthitt1](https://rubytalk.org/u/David_Douthitt1)\
**Post date:** [14 July 2005 23:15 UTC](https://rubytalk.org/t/perl-tk-checkbuttons-and-modifying-variables/19534/6 "2005-07-14T23:15:51Z")

</div>

That was it exactly. Thanks!

Now..... where was that documented again? 😉

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.

---

<div class="post-metadata">

**Author:** ![Hidetoshi\_NAGAI](https://avatars.discourse-cdn.com/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://rubytalk.org/u/Hidetoshi_NAGAI)\
**Post date:** [15 July 2005 05:52 UTC](https://rubytalk.org/t/perl-tk-checkbuttons-and-modifying-variables/19534/7 "2005-07-15T05:52:32Z")

</div>

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 }" 🙂

> **···**
>
> 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)

---

<div class="post-metadata">

**Author:** ![J-Van](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@J-Van](https://rubytalk.org/u/J-Van)\
**Post date:** [15 July 2005 02:03 UTC](https://rubytalk.org/t/perl-tk-checkbuttons-and-modifying-variables/19534/8 "2005-07-15T02:03:07Z")

</div>

They don't?

[http://perlhelp.web.cern.ch/PerlHelp/site/lib/Tk/Checkbutton.html](http://perlhelp.web.cern.ch/PerlHelp/site/lib/Tk/Checkbutton.html)

WIDGET-SPECIFIC OPTIONS (for checkbutton)  
Name: variable  
Class: Variable  
Switch: -variable  
&nbsp;&nbsp;&nbsp;&nbsp;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](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? 😉
> > 
> > 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.

---

<div class="post-metadata">

**Author:** ![Hidetoshi\_NAGAI](https://avatars.discourse-cdn.com/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://rubytalk.org/u/Hidetoshi_NAGAI)\
**Post date:** [15 July 2005 05:46 UTC](https://rubytalk.org/t/perl-tk-checkbuttons-and-modifying-variables/19534/9 "2005-07-15T05:46:01Z")

</div>

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.  
> -------------------------------------------------  
> -----------+-----------+-----------  
> &nbsp;&nbsp;&nbsp;type | get | set  
> ===========+===========+===========  
> &nbsp;&nbsp;String | value | value=  
> -----------+-----------+-----------  
> &nbsp;&nbsp;Symbol | symbol | symbol=  
> -----------+-----------+-----------  
> &nbsp;&nbsp;Numeric | numeric | numeric=  
> -----------+-----------+-----------  
> &nbsp;&nbsp;Boolean | bool | bool=  
> -----------+-----------+-----------  
> &nbsp;&nbsp;List | list | list=  
> -----------+-----------+-----------  
> &nbsp;&nbsp;Num-List | numlist | numlist=  
> -----------+-----------+-----------  
> &nbsp;&nbsp;Variable | variable | variable=  
> -----------+-----------+-----------  
> &nbsp;&nbsp;Widget | window | window=  
> -----------+-----------+-----------  
> &nbsp;&nbsp;Procedure | procedure | procedure=  
> -----------+-----------+-----------
> 
> \* 'List' means that is a list of strings, and 'Num-List' means that  
> &nbsp;&nbsp;&nbsp;is a list of Numeric values.
> 
> \* 'Variable' means that its value is a Tcl's variable name.  
> &nbsp;&nbsp;&nbsp;TkVariable#variable returns a TkVariable object.
> 
> \* 'Widget' means that its value is a widget path.  
> &nbsp;&nbsp;&nbsp;TkVariable#window returns a widget object.
> 
> \* 'Procedure' means that its value is a script.  
> &nbsp;&nbsp;&nbsp;If set a proc object to the variable, TkVariable#procedure returns  
> &nbsp;&nbsp;&nbsp;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)
