Perl/Tk, CheckButtons, and modifying variables

Discussion in 'Ruby' started by David Douthitt, Jul 14, 2005.

  1. 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...
     
    David Douthitt, Jul 14, 2005
    #1
    1. Advertising

  2. David Douthitt

    Joe Van Dyk Guest

    On 7/14/05, David Douthitt <> wrote:
    > 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.
     
    Joe Van Dyk, Jul 14, 2005
    #2
    1. Advertising

  3. 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.
     
    David Douthitt, Jul 14, 2005
    #3
  4. 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...
     
    David Douthitt, Jul 14, 2005
    #4
  5. David Douthitt

    Joe Van Dyk Guest

    On 7/14/05, David Douthitt <> wrote:
    > 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
     
    Joe Van Dyk, Jul 14, 2005
    #5
  6. 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.
     
    David Douthitt, Jul 15, 2005
    #6
  7. David Douthitt

    Joe Van Dyk Guest

    On 7/14/05, David Douthitt <> wrote:
    > 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.
     
    Joe Van Dyk, Jul 15, 2005
    #7
  8. From: "David Douthitt" <>
    Subject: Re: Perl/Tk, CheckButtons, and modifying variables
    Date: Fri, 15 Jul 2005 08:15:51 +0900
    Message-ID: <>
    > 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,
    -----------------------------------
    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 ()
     
    Hidetoshi NAGAI, Jul 15, 2005
    #8
  9. From: Joe Van Dyk <>
    Subject: Re: Perl/Tk, CheckButtons, and modifying variables
    Date: Fri, 15 Jul 2005 07:43:55 +0900
    Message-ID: <>
    > TkTimer.start 1000, -1, proc { $var.value = $var.value == "0" ? 1 : 0 }


    Or, "TkTimer.start 1000, -1, proc { $var.bool ^= true }" :)
    --
    Hidetoshi NAGAI ()
     
    Hidetoshi NAGAI, Jul 15, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank Chen
    Replies:
    3
    Views:
    522
    Cameron Laird
    Oct 8, 2003
  2. William Gill

    a list of Checkbuttons

    William Gill, Aug 30, 2005, in forum: Python
    Replies:
    0
    Views:
    294
    William Gill
    Aug 30, 2005
  3. valen1260

    putting checkbuttons in a listbox

    valen1260, Dec 20, 2005, in forum: Python
    Replies:
    1
    Views:
    271
    Martin Franklin
    Dec 21, 2005
  4. Gigs_
    Replies:
    2
    Views:
    353
    Eric Brunel
    Feb 21, 2007
  5. Joe Van Dyk

    Tkmenubar and checkbuttons

    Joe Van Dyk, May 5, 2005, in forum: Ruby
    Replies:
    4
    Views:
    114
    Joe Van Dyk
    May 5, 2005
Loading...

Share This Page