# Ruby/Tk : Accessing a Checkbox

**URL:** <https://www.ruby-forum.com/t/ruby-tk-accessing-a-checkbox/72313>\
**Category:** Ruby\
**Created:** [February 23, 2006, 2:49pm UTC](https://www.ruby-forum.com/t/ruby-tk-accessing-a-checkbox/72313 "2006-02-23T14:49:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 23, 2006, 2:49pm UTC](https://www.ruby-forum.com/t/ruby-tk-accessing-a-checkbox/72313/1 "2006-02-23T14:49:13Z")

</div>

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

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

$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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 23, 2006, 3:19pm UTC](https://www.ruby-forum.com/t/ruby-tk-accessing-a-checkbox/72313/2 "2006-02-23T15:19:39Z")

</div>

> 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  
variable var  
pack  
end

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 23, 2006, 4:11pm UTC](https://www.ruby-forum.com/t/ruby-tk-accessing-a-checkbox/72313/3 "2006-02-23T16:11:08Z")

</div>

From: removed\_email\_address@domain.invalid  
Subject: Re: Ruby/Tk : Accessing a Checkbox  
Date: Thu, 23 Feb 2006 23:19:18 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

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

## 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”  
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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 23, 2006, 6:09pm UTC](https://www.ruby-forum.com/t/ruby-tk-accessing-a-checkbox/72313/4 "2006-02-23T18:09:45Z")

</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.
