Perl/Tk, CheckButtons, and modifying variables

D

David Douthitt

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

Joe Van Dyk

I'm trying to use a series of CheckButtons, and all is going pretty
well.
=20
However, I want to change the associated variable and have it reflected
in the CheckButton. I'm not sure of how to do this.
=20
I searched through Google Groups and Google Web and found nothing.
=20
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).
=20
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?
=20
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 =3D TkVariable.new
# create a checkbox assining its tkvariable to my_tk_checkbox_var
my_tk_checkbox_var.value =3D 1 # or maybe "1", i forget

That should check the checkbox.
 
D

David Douthitt

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

David Douthitt

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

Joe Van Dyk

I think I'm getting closer (at least) to understanding this. Instead
of:
=20
$serverVariable["server"] =3D 1
=20
....what I want is, in reality:
=20
lvalue_of_what_is_pointed_to_by($serverVariable["server"]) =3D 1

You mean,
$serverVariable["server"].value =3D 1
?
=20
....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.
=20
But how? I'm still trying...

Maybe this will help?

require 'tk'

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

David Douthitt

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

Joe Van Dyk

That was it exactly. Thanks!
=20
Now..... where was that documented again? ;-)
=20
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.

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

Hidetoshi NAGAI

From: "David Douthitt" <[email protected]>
Subject: Re: Perl/Tk, CheckButtons, and modifying variables
Date: Fri, 15 Jul 2005 08:15:51 +0900
Message-ID: said:
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,
-----------------------------------
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,
 
H

Hidetoshi NAGAI

From: Joe Van Dyk <[email protected]>
Subject: Re: Perl/Tk, CheckButtons, and modifying variables
Date: Fri, 15 Jul 2005 07:43:55 +0900
Message-ID: said:
TkTimer.start 1000, -1, proc { $var.value = $var.value == "0" ? 1 : 0 }

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

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top