• Enable/Disable entry widget with checkbutton.

    From snosniv@21:1/5 to All on Thu Mar 2 03:07:06 2023
    I have the following code snippet for an application I've built for my own use. Everything currently works, but it would be 'nicer' if the entry widgets were only accessible/writable once the checkbutton was ticked.

    Do I need an 'event' or can I enable/disable the entry widgets with some sort of command after the checkbutton?

    I currently use the var LO_TM_ramp elsewhere in the program, does this need to change function?

    TIA, KevP.

    checkbutton $f3a.cbRCR_TM_RMP -text "On/Off" -var LO_TM_ramp -font {Arial 8 bold} -background white -foreground red

    entry $f3a.enMINS_LOPWRb -textvar int_mins_LOb -width 2 -validate all -vcmd {ValidInt %P}
    entry $f3a.enSECS_LOPWRb -textvar int_secs_LOb -width 2 -validate all -vcmd {ValidInt %P}

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Arjen Markus@21:1/5 to snosniv on Thu Mar 2 05:33:11 2023
    On Thursday, March 2, 2023 at 12:07:09 PM UTC+1, snosniv wrote:
    I have the following code snippet for an application I've built for my own use. Everything currently works, but it would be 'nicer' if the entry widgets were only accessible/writable once the checkbutton was ticked.

    Do I need an 'event' or can I enable/disable the entry widgets with some sort of command after the checkbutton?

    I currently use the var LO_TM_ramp elsewhere in the program, does this need to change function?

    TIA, KevP.

    checkbutton $f3a.cbRCR_TM_RMP -text "On/Off" -var LO_TM_ramp -font {Arial 8 bold} -background white -foreground red

    entry $f3a.enMINS_LOPWRb -textvar int_mins_LOb -width 2 -validate all -vcmd {ValidInt %P}
    entry $f3a.enSECS_LOPWRb -textvar int_secs_LOb -width 2 -validate all -vcmd {ValidInt %P}

    You can use the -command option to the checkbutton for this. It takes a Tcl command as its value. In that command/procedure you can then enable or disable the entry widgets.

    Regards,

    Arjen

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to snosniv on Thu Mar 2 14:43:11 2023
    snosniv <nivparsons@gmail.com> wrote:
    I have the following code snippet for an application I've built for
    my own use. Everything currently works, but it would be 'nicer' if
    the entry widgets were only accessible/writable once the checkbutton
    was ticked.

    Do I need an 'event' or can I enable/disable the entry widgets with
    some sort of command after the checkbutton?

    I currently use the var LO_TM_ramp elsewhere in the program, does
    this need to change function?

    TIA, KevP.

    checkbutton $f3a.cbRCR_TM_RMP -text "On/Off" -var LO_TM_ramp -font {Arial 8 bold} -background white -foreground red

    entry $f3a.enMINS_LOPWRb -textvar int_mins_LOb -width 2 -validate all -vcmd {ValidInt %P}
    entry $f3a.enSECS_LOPWRb -textvar int_secs_LOb -width 2 -validate all -vcmd {ValidInt %P}

    Doing this kind of 'cross widget' interaction is one of the uses for
    the '-command' option to the widgets. Example:

    proc toggle {checkbutton entrywidget} {
    if {[set $checkbutton]} {
    $entrywidget configure -state normal
    } else {
    $entrywidget configure -state disabled
    }
    }

    checkbutton .cb1 -variable ::cb1 -onvalue 1 -offvalue 0 \
    -command [list toggle ::cb1 .ew1]

    checkbutton .cb2 -variable ::cb2 -onvalue 1 -offvalue 0 \
    -command [list toggle ::cb2 .ew2]

    entry .ew1 -state disabled

    entry .ew2 -state disabled

    grid .cb1 .ew1
    grid .cb2 .ew2

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From snosniv@21:1/5 to Rich on Thu Mar 2 07:59:07 2023
    On Thursday, 2 March 2023 at 14:43:15 UTC, Rich wrote:
    snosniv <nivpa...@gmail.com> wrote:
    I have the following code snippet for an application I've built for
    my own use. Everything currently works, but it would be 'nicer' if
    the entry widgets were only accessible/writable once the checkbutton
    was ticked.

    Do I need an 'event' or can I enable/disable the entry widgets with
    some sort of command after the checkbutton?

    I currently use the var LO_TM_ramp elsewhere in the program, does
    this need to change function?

    TIA, KevP.

    checkbutton $f3a.cbRCR_TM_RMP -text "On/Off" -var LO_TM_ramp -font {Arial 8 bold} -background white -foreground red

    entry $f3a.enMINS_LOPWRb -textvar int_mins_LOb -width 2 -validate all -vcmd {ValidInt %P}
    entry $f3a.enSECS_LOPWRb -textvar int_secs_LOb -width 2 -validate all -vcmd {ValidInt %P}
    Doing this kind of 'cross widget' interaction is one of the uses for
    the '-command' option to the widgets. Example:

    proc toggle {checkbutton entrywidget} {
    if {[set $checkbutton]} {
    $entrywidget configure -state normal
    } else {
    $entrywidget configure -state disabled
    }
    }

    checkbutton .cb1 -variable ::cb1 -onvalue 1 -offvalue 0 \
    -command [list toggle ::cb1 .ew1]

    checkbutton .cb2 -variable ::cb2 -onvalue 1 -offvalue 0 \
    -command [list toggle ::cb2 .ew2]

    entry .ew1 -state disabled

    entry .ew2 -state disabled

    grid .cb1 .ew1
    grid .cb2 .ew2


    OK, thanks for the help, but run into a different issue now.
    I used to have a variable on the checkbutton which was later tested to see if the entry widgets had valid data, if not a message was flagged,
    but I can't have 2 variables on the checkbutton , so how do I overcome this? Also, do I need the -onvalue 1 -offvalue 0, I thought ticked was 1 & unticked was 0 by default for the variable.
    This is my code so far (snippet), it does enable/disable the entry widgets nicely, but I no longer have my previous -var "LO_TM_ramp" that is used later in the code.
    So what do I use instead, or can I use my original var somehow?
    Note that I enable/disable TWO entry widgets with ONE checkbutton (i.e. Mins & Secs for an Interval recovery phase [indoor cycling]!).

    Any help very much appreciated.

    ##----------------------------------------------------------------------------- ## LO power label & entry positions for VARIABLE TIME INTERVALS ##----------------------------------------------------------------------------- label $f3a.lbLO_TIMb -text "Rcvr_Time (end)" -font {Arial 8 bold} -background white -foreground red
    grid $f3a.lbLO_TIMb -row 6 -column 0
    entry $f3a.enMINS_LOPWRb -textvar int_mins_LOb -width 2 -validate all -vcmd {ValidInt %P} -state disabled
    entry $f3a.enSECS_LOPWRb -textvar int_secs_LOb -width 2 -validate all -vcmd {ValidInt %P} -state disabled

    ##### checkbutton $f3a.cbRCR_TM_RMP -text "On/Off" -var LO_TM_ramp -font {Arial 8 bold} -background white -foreground red

    checkbutton $f3a.cbRCR_TM_RMP -text "On/Off" -font {Arial 8 bold} \
    -background white -foreground red \
    -variable ::$f3a.cbRCR_TM_RMP -onvalue 1 -offvalue 0 \
    -command [list toggle ::$f3a.cbRCR_TM_RMP $f3a.enMINS_LOPWRb $f3a.enSECS_LOPWRb]

    #### This now works to enable/disable the mins/secs entry widgets, but I've lost the -var LO_TM_ramp I use later in the program. :-(

    grid $f3a.enMINS_LOPWRb -row 6 -column 1
    grid $f3a.enSECS_LOPWRb -row 6 -column 3
    grid $f3a.cbRCR_TM_RMP -row 6 -column 6

    ##----------------------------------------------------------------------------- ## proc to enable/disable the entry widgets on checkbox set/unset. ##----------------------------------------------------------------------------- proc toggle {checkbutton entrywidget1 entrywidget2} {
    if {[set $checkbutton]} {
    $entrywidget1 configure -state normal
    $entrywidget2 configure -state normal
    } else {
    $entrywidget1 configure -state disabled
    $entrywidget2 configure -state disabled
    }
    } ##-----------------------------------------------------------------------------

    TIA, KevP.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ralf Fassel@21:1/5 to All on Thu Mar 2 18:00:16 2023
    * snosniv <nivparsons@gmail.com>
    | I used to have a variable on the checkbutton which was later tested to
    | see if the entry widgets had valid data, if not a message was flagged,
    | but I can't have 2 variables on the checkbutton , so how do I overcome
    | this?
    --<snip-snip>--
    | This is my code so far (snippet), it does enable/disable the entry
    | widgets nicely, but I no longer have my previous -var "LO_TM_ramp"

    Why did you remove the
    -var "LO_TM_ramp"
    and added the
    -variable ::$f3a.cbRCR_TM_RMP
    for the checkbutton?

    IMHO you just could have kept the LO_TM_ramp and test that in
    the 'toggle' proc.

    | Also, do I need the -onvalue 1 -offvalue 0, I thought ticked was 1 &
    | unticked was 0 by default for the variable.

    You don't need them. Sometimes it is preferred to also note the
    default values in the code, so there is no doubt what is actually used.
    For the default values you would need to look them up in the manual.

    HTH
    R'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From snosniv@21:1/5 to Ralf Fassel on Thu Mar 2 09:29:45 2023
    On Thursday, 2 March 2023 at 17:00:21 UTC, Ralf Fassel wrote:
    * snosniv <nivpa...@gmail.com>
    | I used to have a variable on the checkbutton which was later tested to
    | see if the entry widgets had valid data, if not a message was flagged,
    | but I can't have 2 variables on the checkbutton , so how do I overcome
    | this?
    --<snip-snip>--
    | This is my code so far (snippet), it does enable/disable the entry
    | widgets nicely, but I no longer have my previous -var "LO_TM_ramp"
    Why did you remove the
    -var "LO_TM_ramp"
    and added the
    -variable ::$f3a.cbRCR_TM_RMP
    for the checkbutton?

    IMHO you just could have kept the LO_TM_ramp and test that in
    the 'toggle' proc.
    | Also, do I need the -onvalue 1 -offvalue 0, I thought ticked was 1 &
    | unticked was 0 by default for the variable.
    You don't need them. Sometimes it is preferred to also note the
    default values in the code, so there is no doubt what is actually used.
    For the default values you would need to look them up in the manual.

    HTH
    R'

    Thanks Ralf & others, all seems functioning now.
    Much appreciated.
    KevP

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to snosniv on Thu Mar 2 20:47:17 2023
    snosniv <nivparsons@gmail.com> wrote:
    OK, thanks for the help, but run into a different issue now.

    I used to have a variable on the checkbutton which was later tested
    to see if the entry widgets had valid data, if not a message was
    flagged,

    Given that the checkbutton variable is controlled by the checkbutton,
    it can't simultaneoulsy have held the entry data as well.

    but I can't have 2 variables on the checkbutton , so how do I
    overcome this?

    Three possibilities:

    1) A second variable tied to the entry widget, and the
    'enable/disable' command also checks the second variable for
    "validity"

    2) Pass in the relevant entry name and use the "$entry get"
    subcommand to retreive the entry contents, then check for
    "validity".

    3) Setup the native entry widget 'validator' subsystem to perform
    validation on the entry widget (see man page for the entry widget
    for details on the native validation subsystem).

    Also, do I need the -onvalue 1 -offvalue 0, I thought ticked was 1 & unticked was 0 by default for the variable.

    They are, I just included them to make explicit in the simple example.

    This is my code so far (snippet), it does enable/disable the entry
    widgets nicely, but I no longer have my previous -var "LO_TM_ramp"
    that is used later in the code.
    So what do I use instead, or can I use my original var somehow?

    If you want to use a variable, then attach a variable to the relevant
    entry.

    Note that I enable/disable TWO entry widgets with ONE checkbutton
    (i.e. Mins & Secs for an Interval recovery phase [indoor cycling]!).

    The command triggered by changing the checkbutton state can do anything
    you program it to do. You just have to write the code to do whatever
    it is you want to do.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Mike Griffiths@21:1/5 to snosniv on Thu Mar 2 16:03:02 2023
    On Thursday, 2 March 2023 at 15:59:09 UTC, snosniv wrote:
    On Thursday, 2 March 2023 at 14:43:15 UTC, Rich wrote:
    snosniv <nivpa...@gmail.com> wrote:
    I have the following code snippet for an application I've built for
    my own use. Everything currently works, but it would be 'nicer' if
    the entry widgets were only accessible/writable once the checkbutton
    was ticked.

    Do I need an 'event' or can I enable/disable the entry widgets with
    some sort of command after the checkbutton?

    I currently use the var LO_TM_ramp elsewhere in the program, does
    this need to change function?

    TIA, KevP.

    checkbutton $f3a.cbRCR_TM_RMP -text "On/Off" -var LO_TM_ramp -font {Arial 8 bold} -background white -foreground red

    entry $f3a.enMINS_LOPWRb -textvar int_mins_LOb -width 2 -validate all -vcmd {ValidInt %P}
    entry $f3a.enSECS_LOPWRb -textvar int_secs_LOb -width 2 -validate all -vcmd {ValidInt %P}
    Doing this kind of 'cross widget' interaction is one of the uses for
    the '-command' option to the widgets. Example:

    proc toggle {checkbutton entrywidget} {
    if {[set $checkbutton]} {
    $entrywidget configure -state normal
    } else {
    $entrywidget configure -state disabled
    }
    }

    checkbutton .cb1 -variable ::cb1 -onvalue 1 -offvalue 0 \
    -command [list toggle ::cb1 .ew1]

    checkbutton .cb2 -variable ::cb2 -onvalue 1 -offvalue 0 \
    -command [list toggle ::cb2 .ew2]

    entry .ew1 -state disabled

    entry .ew2 -state disabled

    grid .cb1 .ew1
    grid .cb2 .ew2
    OK, thanks for the help, but run into a different issue now.
    I used to have a variable on the checkbutton which was later tested to see if the entry widgets had valid data, if not a message was flagged,
    but I can't have 2 variables on the checkbutton , so how do I overcome this? Also, do I need the -onvalue 1 -offvalue 0, I thought ticked was 1 & unticked was 0 by default for the variable.
    This is my code so far (snippet), it does enable/disable the entry widgets nicely, but I no longer have my previous -var "LO_TM_ramp" that is used later in the code.
    So what do I use instead, or can I use my original var somehow?
    Note that I enable/disable TWO entry widgets with ONE checkbutton (i.e. Mins & Secs for an Interval recovery phase [indoor cycling]!).

    Any help very much appreciated.

    ##-----------------------------------------------------------------------------
    ## LO power label & entry positions for VARIABLE TIME INTERVALS ##-----------------------------------------------------------------------------
    label $f3a.lbLO_TIMb -text "Rcvr_Time (end)" -font {Arial 8 bold} -background white -foreground red
    grid $f3a.lbLO_TIMb -row 6 -column 0
    entry $f3a.enMINS_LOPWRb -textvar int_mins_LOb -width 2 -validate all -vcmd {ValidInt %P} -state disabled
    entry $f3a.enSECS_LOPWRb -textvar int_secs_LOb -width 2 -validate all -vcmd {ValidInt %P} -state disabled

    ##### checkbutton $f3a.cbRCR_TM_RMP -text "On/Off" -var LO_TM_ramp -font {Arial 8 bold} -background white -foreground red

    checkbutton $f3a.cbRCR_TM_RMP -text "On/Off" -font {Arial 8 bold} \ -background white -foreground red \
    -variable ::$f3a.cbRCR_TM_RMP -onvalue 1 -offvalue 0 \
    -command [list toggle ::$f3a.cbRCR_TM_RMP $f3a.enMINS_LOPWRb $f3a.enSECS_LOPWRb]

    #### This now works to enable/disable the mins/secs entry widgets, but I've lost the -var LO_TM_ramp I use later in the program. :-(

    grid $f3a.enMINS_LOPWRb -row 6 -column 1
    grid $f3a.enSECS_LOPWRb -row 6 -column 3
    grid $f3a.cbRCR_TM_RMP -row 6 -column 6

    ##-----------------------------------------------------------------------------
    ## proc to enable/disable the entry widgets on checkbox set/unset. ##-----------------------------------------------------------------------------
    proc toggle {checkbutton entrywidget1 entrywidget2} {
    if {[set $checkbutton]} {
    $entrywidget1 configure -state normal
    $entrywidget2 configure -state normal
    } else {
    $entrywidget1 configure -state disabled
    $entrywidget2 configure -state disabled
    }
    } ##-----------------------------------------------------------------------------

    TIA, KevP.

    Just a few thoughts:
    * For more modularity, I would consider making the last arg for [toggle] a list (or using the special "args" last argument which automatically returns a list of all additional arguments) and using [foreach] to loop through, rather than having two
    specific args for different entry widgets; you can then have any number of entry widgets controlled by the proc with no code changes or duplicate copies of the proc needed
    * Your [toggle] proc currently relies on being passed an absolute namespace reference for the variable (::foo rather than foo, etc) and will error out without this. For robustness, I would suggest using [upvar] or a similar approach to guarantee you're
    looking at the variable in the global namespace.
    * Using -validate on the entry widgets can prevent them from ever containing invalid input (if you use -validate key), but you can use the -validatecommand to set a variable for whether the input is valid or not if using other validation methods that do
    allow invalid input such as -validate focus

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