On and Off Values for a Checkbutton

Depending on how you want the Checkbutton to interact with the rest of your application, sometimes it makes sense to use different values instead of 0 and 1. If you don’t like the default value of 1, you can use the -onvalue option to change it:

-onvalue => newvalue  ## Default is 1

Similarly, if you want to use something other than 0 for an off value, use -offvalue:

-offvalue => newvalue  ## Default is 0

The newvalue could be anything, as long as it is a scalar value. This means you can use references to arrays and hashes if you really want to.

It is good practice to keep the meaning of -onvalue the opposite of -offvalue. If -onvalue is now the string "ON", logically -offvalue should be "OFF". Of course, if the purpose of this Checkbutton is to use a more accurate value of pi, then -onvalue could be "3.14159265359" and -offvalue could be "3.14".

Be careful when you use unusual values for -onvalue and -offvalue. If you set the variable to something that doesn’t equal either one of them, the Checkbutton will be considered off, even though the value of the $variable will not equal the -offvalue.For instance, if you set -onvalue => 1, -offvalue => 0, and you set $variable to 3, the Checkbutton will be considered off.

Get Mastering Perl/Tk now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.