• Re: How to set ttk::radiobutton -variable to an object's instance varia

    From Schelte@21:1/5 to Mark Summerfield on Fri Jul 11 13:28:06 2025
    On 11/07/2025 11:55, Mark Summerfield wrote:
    Given this class:

    oo::class create App {
    # ...
    variable ShowState
    }

    And this method:

    oo::define App method make_controls {} {
    # ...
    ttk::radiobutton .controlsFrame.showFrame.asIsRadio \
    -text "Show as-is" -value asis -variable ShowState
    set ShowState asis

    The variable given to the radio button and the instance variable are_different_.

    ttk::radiobutton .controlsFrame.showFrame.asIsRadio \
    -text "Show as-is" -value asis -variable [my varname ShowState]


    Schelte.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Schelte@21:1/5 to Mark Summerfield on Fri Jul 11 21:12:08 2025
    On 11/07/2025 17:21, Mark Summerfield wrote:
    That silently didn't work.

    Then you have a bug somewhere in the code you didn't show, because [my
    varname ShowState] correctly produces the fully qualified name of the
    instance variable.

    Another way to get the fully qualified name of the variable is
    [namespace which -variable ShowState]. But as that produces the same
    result, it also won't work until you fix that other bug.


    Schelte.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Emiliano@21:1/5 to Schelte on Fri Jul 11 20:13:59 2025
    On Fri, 11 Jul 2025 21:12:08 +0200
    Schelte <[email protected]> wrote:

    ...
    Another way to get the fully qualified name of the variable is
    [namespace which -variable ShowState]. But as that produces the same
    result, it also won't work until you fix that other bug.

    Adding to what Schelte correctly pointed out, these are your options:

    * [my varname somevar]: This works inside TclOO objects, and works for scalar
    variables, arrays and array elements.
    * [namespace which -variable somevar]: This works inside TclOO objects and
    namespaces. Works for scalar variables and arrays, but not for array
    elements. See Tcl bug#472113 . If you need an array element, use
    [namespace which -variable somearray](element).
    * last but not least, [namespace current]::somevar works if somevar is
    a variable in the current namespace, including the namespace of any TclOO
    object, and works for scalar, array and array elements.

    Regards
    --
    Emiliano

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From et99@21:1/5 to Emiliano on Fri Jul 11 17:28:11 2025
    On 7/11/2025 4:13 PM, Emiliano wrote:
    On Fri, 11 Jul 2025 21:12:08 +0200
    Schelte <[email protected]> wrote:

    ...
    Another way to get the fully qualified name of the variable is
    [namespace which -variable ShowState]. But as that produces the same
    result, it also won't work until you fix that other bug.

    Adding to what Schelte correctly pointed out, these are your options:

    * [my varname somevar]: This works inside TclOO objects, and works for scalar
    variables, arrays and array elements.
    * [namespace which -variable somevar]: This works inside TclOO objects and
    namespaces. Works for scalar variables and arrays, but not for array
    elements. See Tcl bug#472113 . If you need an array element, use
    [namespace which -variable somearray](element).
    * last but not least, [namespace current]::somevar works if somevar is
    a variable in the current namespace, including the namespace of any TclOO
    object, and works for scalar, array and array elements.

    Regards


    As a learning exercise,

    I filled in Mark's code (added the 2 frames, a second radio button, and packed it up) and used the [my varname ...] form which worked for me.

    Here's a little debug procedure one can use to find object variables. It might prove helpful. Note, the [my varname ...] will register the variable in the object's namespace, but the variable is still unset, which this code can show.



    # Function to dump all object variables
    proc dovar {{all 0} {sep "-----------------------------------"} } {
    if { $sep ne "" } {
    puts $sep
    }
    foreach ns [namespace children ::oo] {
    if {[string match "Obj*" [namespace tail $ns]]} {
    set objName ::$ns
    set class "<unknown>"
    catch {
    set class [info object class $objName]
    }
    if {!$all && $class eq "<unknown>"} {
    continue
    }
    puts "Object: $objName (Class: $class)"
    foreach var [info vars ${ns}::*] {
    set shortName [namespace tail $var]
    if {[info exists $var]} {
    catch { set val [set $var] }
    } else {
    set val "<unset>"
    }
    puts " $shortName ($var) = $val"
    }
    }
    }
    if { $sep ne "" } {
    puts $sep
    }
    }


    -eric

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)