# Ruby/Tk : Accessing a Checkbox

**URL:** <https://rubytalk.org/t/ruby-tk-accessing-a-checkbox/25172>\
**Category:** ruby-talk\
**Created:** [23 February 2006 13:48 UTC](https://rubytalk.org/t/ruby-tk-accessing-a-checkbox/25172 "2006-02-23T13:48:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![U235321044](https://avatars.discourse-cdn.com/v4/letter/u/50afbb/32.png) [@U235321044](https://rubytalk.org/u/U235321044)\
**Post date:** [23 February 2006 13:48 UTC](https://rubytalk.org/t/ruby-tk-accessing-a-checkbox/25172/1 "2006-02-23T13:48:32Z")

</div>

How can I find out whether a Checkbox is checked or unchecked in Ruby?

Here is my Checkbox definition (hope it is correct):

&nbsp;&nbsp;$bpmto\_check=TkCheckButton.new(bpmto\_frame);

How can I find the state of the checkbox? I tried the .value  
member (like in the entry field), but this is not defined  
for checkboxes.

I have searched several tutorials on Ruby/Tk, but did not find  
one which covers checkboxes. Is there a reference manual  
available, or man pages, which covers the Tk extensions of Ruby?

Ronald

> **···**
>
> --  
> Sent by [mn-pg-p-e-b-consultant-3.com](http://mn-pg-p-e-b-consultant-3.com) from siemens subdomain of com  
> This is a spam protected message. Please answer with reference header.  
> Posted via [http://www.usenet-replayer.com](http://www.usenet-replayer.com)

---

<div class="post-metadata">

**Author:** ![Dperkins](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@Dperkins](https://rubytalk.org/u/Dperkins)\
**Post date:** [23 February 2006 14:19 UTC](https://rubytalk.org/t/ruby-tk-accessing-a-checkbox/25172/2 "2006-02-23T14:19:18Z")

</div>

> How can I find out whether a Checkbox is checked or unchecked in Ruby?
> 
> Here is my Checkbox definition (hope it is correct):
> 
> &nbsp;&nbsp;$bpmto\_check=TkCheckButton.new(bpmto\_frame);
> 
> How can I find the state of the checkbox? I tried the .value  
> member (like in the entry field), but this is not defined  
> for checkboxes.
> 
> I have searched several tutorials on Ruby/Tk, but did not find  
> one which covers checkboxes. Is there a reference manual  
> available, or man pages, which covers the Tk extensions of Ruby?
> 
> Ronald

You need a TkVariable associated with the checkbutton.

var = TkVariable.new

$pbmto\_check = TkCheckButton.new(pbmto\_frame) do  
&nbsp;&nbsp;&nbsp;&nbsp;variable var  
&nbsp;&nbsp;&nbsp;&nbsp;pack  
end

---

<div class="post-metadata">

**Author:** ![Hidetoshi\_NAGAI](https://avatars.discourse-cdn.com/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://rubytalk.org/u/Hidetoshi_NAGAI)\
**Post date:** [23 February 2006 15:10 UTC](https://rubytalk.org/t/ruby-tk-accessing-a-checkbox/25172/3 "2006-02-23T15:10:46Z")

</div>

Message-ID: \<1311.63.77.210.130.1140704341.squirrel@users.frii.com\>

> You need a TkVariable associated with the checkbutton.
> 
> var = TkVariable.new
> 
> $pbmto\_check = TkCheckButton.new(pbmto\_frame) do  
> &nbsp;&nbsp;&nbsp;&nbsp;variable var  
> &nbsp;&nbsp;&nbsp;&nbsp;pack  
> end

Latest Ruby/Tk supports a default variable of a checkbutton.  
So, you don't need define a TkVariable. For example,

> **···**
>
> From: dperkins@frii.com  
> Subject: Re: Ruby/Tk : Accessing a Checkbox  
> Date: Thu, 23 Feb 2006 23:19:18 +0900  
> -------------------------------------------------------------  
> irb(main):002:0\> $pbmto\_check = TkCheckButton.new(pbmto\_frame)  
> =\> #\<TkCheckButton:0xb7c48a8c @path=".w00000.w00001"\>  
> irb(main):003:0\> $pbmto\_check.get\_value  
> =\> "0"  
> irb(main):004:0\> $pbmto\_check.set\_value(1)  
> =\> "1"  
> irb(main):005:0\> $pbmto\_check.get\_value  
> =\> "1"  
> irb(main):006:0\> $pbmto\_check.variable  
> =\> #\<TkVariable: w00001\>  
> irb(main):007:0\> $pbmto\_check.variable.value  
> =\> "1"  
> irb(main):008:0\> TkUtil.bool($pbmto\_check.get\_value)  
> =\> true  
> irb(main):009:0\> $pbmto\_check.variable.bool  
> =\> true  
> irb(main):010:0\> $pbmto\_check.variable.value = 0  
> =\> 0  
> irb(main):011:0\> $pbmto\_check.get\_value  
> =\> "0"  
> irb(main):012:0\> TkUtil.bool($pbmto\_check.get\_value)  
> =\> false  
> irb(main):013:0\> $pbmto\_check.variable.value  
> =\> "0"  
> irb(main):014:0\> $pbmto\_check.variable.bool  
> =\> false  
> irb(main):015:0\> $pbmto\_check.variable.numeric  
> =\> 0  
> -------------------------------------------------------------  
> --  
> Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

---

<div class="post-metadata">

**Author:** ![Dperkins](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@Dperkins](https://rubytalk.org/u/Dperkins)\
**Post date:** [23 February 2006 17:08 UTC](https://rubytalk.org/t/ruby-tk-accessing-a-checkbox/25172/4 "2006-02-23T17:08:03Z")

</div>

> Latest Ruby/Tk supports a default variable of a checkbutton.  
> So, you don't need define a TkVariable. For example,  
> -------------------------------------------------------------  
> irb(main):002:0\> $pbmto\_check = TkCheckButton.new(pbmto\_frame)  
> =\> #\<TkCheckButton:0xb7c48a8c @path=".w00000.w00001"\>  
> irb(main):003:0\> $pbmto\_check.get\_value  
> =\> "0"

Great! I'll add this to my Rubyt/Tk notes.
